Monday, May 24, 2010

How to read a file through writing a C program?

I need to read a file such as abc.txt or abc.xls when i compile a C progam. i.e., i should read a personal file through output of a c program.


I need the Syntax for this Plz!!

How to read a file through writing a C program?
I think guys have given all the function needed to open a text file through C. But i want to remind you that you want be able read the Containts of a Excel or word File using C.
Reply:char buffer[4096];


FILE *f = fopen( full_path_name, "rt" );





while( fgets( buffer, 4096, f ))


{


/* do something with the data you have read. */


}





fclose( f );
Reply:Wel I feel all the coding is here, the correct C approach is by mentioning the stdio.h header


FILE *handle; /* [hope u know what that means; it is a file handle pointer, pointuing to the segment:offset value of the file styruicture prepared inC]*/


/* then use: fscanf(pointer,format,variable) to read and fprintf(pointer, format, variabkle) to print to a file. You can also use fgetc, fgets, fputc, fputs for the same, Use fseek to seek, and ftell to return the file position handle, which is actually long value. Use fwrite to overwrite values indatabases*/


For C++ it uses fstream.h and the coding is


fstream *handler; // for both input and output file operations


ifstream *handler1; // for only input


ofsteram *handler2; // for only output


/* REFER TO C++ documentation for help on this, Iam not too much inclined on C++ programming, sorry! But i know it is way easier in C++, by overloading objects of fstream class by opetators: %26gt;%26gt; and %26lt;%26lt; for input and output operations respectively. CALL me crazy but I prefer C style*/





NOTE: Both styles compile under the standard C compiler


The Excel, Word,Bmp files are just files of spoecial formats, you know the file format and wolla!, you can access and read, modify a Excel, Word, Bmp, etc, files from ur program that Microsoft Windows can easily recogise!
Reply:C has lot of functions for file handling.





1. Open a file by using fopen( ).


ex: FILE *fp=fopen("filename", "r");


2. Char by char reading:


char ch;


while((ch=fgetc(fp)) != null) printf("%c", ch);


3. Reading line by line:


char str[81];


while(fgets(fp,str,81)!= null) printf("%s", str);





after you are done, close the file.


4. fclose(fp);





HTH.
Reply:I don't really know what you want but the code to read a file is simple.


ex . your file can be somthing like this :


data1 value1


data2 value2


data3 value4





let's say that data1,data2,data3 are integers and value1,2,3 are strings (without space between words)





you can read the data like this :





#include%26lt;stdio.h%26gt;


void main(){


int data;


char value[100];


FILE *f=fopen("abc.txt","r");


while(!feof(f)){


fscanf(f,"%d",%26amp;data);


fscanf(f,"%s",%26amp;value);


//now you can process value and data in your interest nd that's it


// for exmple printf("%d %s",data,vlue);


}


}


No comments:

Post a Comment