220 likes | 352 Views
FILE HANDLING in C Language-2 Lecture 27. FILE pointer in C. The statement: FILE *fptr1, *fptr2 ; declares that fptr1 and fptr2 are pointer type variables of type FILE . They can contain the address of a file descriptors . Opening FILE. The statement: FILE *fptr1;
E N D
FILE HANDLING in C Language-2 Lecture 27
FILE pointer in C • The statement: FILE *fptr1, *fptr2 ; declares that fptr1 and fptr2are pointer type variables of type FILE. They can contain the address of a file descriptors.
Opening FILE • The statement: FILE *fptr1; fptr1 = fopen( “d:\\mydata.txt", "r"); would open the file d:\\mydata.txt for input (reading). • The statement: FILE *fptr2; fptr2 = fopen(“d:\\results.txt", "w"); would open the file d:\\results.txt for output (writing).
Opening Text Files • The statement: FILE *fptr2; fptr2 = fopen (“d:\\results.txt",“r+"); would open the file d:\\results.txt for both reading and writing. • Once the FILE is open, it stay openuntil you close it or end of the program reaches (which will close all files.)
Testing for Successful Open • If the FILE was not able to be opened, then the value returned by the fopenroutine is NULL. • For example, let's assume that the file d:\\mydata.txtdoes not exist. Then: FILE *fptr1; fptr1 = fopen ( “d:\\mydata.txt", “W") ; if (fptr1 == NULL) cout<< "File 'mydata' can’t be open”; else cout << “File opens successfully”;
Writing in FILE • We can write any contents (data)in the FILE using the following FILING function: fprintf (paramter1, paramter2, paramter3); FILE pointer Signature of the variable Actual name of the variable
Writing in FILE (Example 1) The fprintf function will write value from the number1 (memory location) to hard disk location(“d:\\mydata.txt”). void main (void) { FILE *fptr; fptr = fopen( “d:\\mydata.txt” , ”w” ); // write mod int number1 = 93; fprintf (fptr,”%d”, number1); }
Writing in FILE (Example 2) The fprintf function will write value from the number1 & floatData (memory location) to hard disk location(“d:\\myfile.dat”). void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”w” ); // write mod long number1 = 93; float floatData = 34.63; fprintf (fptr,”%d%f”, number1,floatData); }
Writing in FILE (Example 3) void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”w”); // write mod long number1 = 93; float floatData = 34.63; char myCharacter = ‘D’; fprintf (fptr,”%d%f%c”, number1,floatData,myCharacter); } The fprintf function will write value from the number1, floatData and myCharacter (memory location) to hard disk location(“d:\\mydata.txt”).
Writing in FILE (Example 4) void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”w” ); // write mod char string1 [40]; strcpy (string1, “My text information is Pakistan”); fprintf (fptr,”%s”, string1); } The fprintf function will write value from the string 1 (memory location) to hard disk location(“d:\\mydata.txt”).
Writing in FILE (problem with “w” MOD) • What would be the final data in the FILE name myfile.data when the above code executes 3 times. • The final data would be only (353) not (353353353). • The reason is that, with “w” MOD, on every run the previous data is REMOVED, and the new data is written frombeginning of FILE. void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”w” ); // write mod int data = 353; fprintf (fptr,”%d”, data); }
Solution Open in Append MOD “a+” or “w+” • In append MOD, the previous data in theFILE is not removed on every new execution. • The data in the FILE name “d:\\myfile.data”on the 3 runs would be now (353353353). void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”a+”); // write mod int data = 353; fprintf (fptr,”%d”, data); }
OUTLINE • How we can read data/information from the FILES. • How we can close the FILES.
Reading from FILE • We can write any contents (data)in the FILE using the following FILING function: returnParameter fscanf (paramter1, paramter2, paramter3); FILE pointer Signature of the variable Actual name of the variable End of file indicator
Reading from FILE (Example 1) The fscanf function will read value from the hard disk location(“d:\\mydata.txt”) to number1 (memory location). void main (void) { FILE *fptr; fptr = fopen( “d:\\mydata.txt” , ”r+” ); // random mod int number1 = 93, number2 = 0; fprintf (fptr,”%d”, number1); fscanf (fptr,”%d”, &number2); }
Reading from FILE (Example 2) void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”r+” ); // random mod long number1 = 93, read1 = 0; float floatData = 34.63, read2 = 0; fprintf (fptr,”%d%f”, number1,floatData); fscanf (fptr,”%d%f”,&read1,&read2); }
Reading from FILE (Example 3) void main (void) { FILE *fptr; fptr = fopen(“d:\\myfile.dat”,”r+”); // random mod long number1 = 93, read1 = 0; float floatData = 34.63, read2 = 0; char myCharacter = ‘D’, read3 = ‘ ‘; fprintf (fptr,”%d%f%c”, number1,floatData,myCharacter); fscanf (fptr,”%d%f%c”,&read1, &read2, &read3); cout << “Value are” << read1 << read2 << read3; }
Reading from FILE (Example 4) void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”r+” ); // random mod char string1 [40], string2 [40]; strcpy (string1, “My text information is Pakistan”); fprintf (fptr,”%s”, string1); fscanf (fptr,”%s”,string2); cout << string2; }
Reading byte by byte Information from FILE (Example 5) void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”w” ); // write mod char string1 [40]; strcpy (string1, “My text information is Pakistan”); fprintf (fptr,”%s”, string1); } write.cpp The problem with this code is that it can read out of the file data from your disk. Control it using ‘End of file’ marker. void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”r” ); // read mod char character; while (1) {fscanf (fptr,”%c”,&character); cout << character; } } read.cpp
End of file • The end-of-file indicator informs the program when there are no more data (no more bytes) to be processed. • There are a number of ways to test for the end-of-file condition. One way is to use the value returned by the fscanffunction: int istatus; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == EOF ) { cout << “End-of-file encountered.”; }
Reading byte by byte Information from FILE (Example 6) read.cpp void main (void) { FILE *fptr; fptr = fopen( “d:\\myfile.dat” , ”r” ); // read mod char character; while (1) { int status = 0; status = fscanf (fptr,”%c”,&character); if ( status == EOF ) break; cout << character; } }
Closing FILES • The statements: fclose ( fptr1 ) ; fclose ( fptr2 ) ; will close the files and release the file descriptor space from memory.