1 / 22

FILE HANDLING in C Language-2 Lecture 27

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;

yonah
Download Presentation

FILE HANDLING in C Language-2 Lecture 27

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILE HANDLING in C Language-2 Lecture 27

  2. 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.

  3. 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).

  4. 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.)

  5. 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”;

  6. 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

  7. 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); }

  8. 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); }

  9. 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”).

  10. 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”).

  11. 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); }

  12. 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); }

  13. OUTLINE • How we can read data/information from the FILES. • How we can close the FILES.

  14. 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

  15. 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); }

  16. 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); }

  17. 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; }

  18. 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; }

  19. 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

  20. 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.”; }

  21. 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; } }

  22. Closing FILES • The statements: fclose ( fptr1 ) ; fclose ( fptr2 ) ; will close the files and release the file descriptor space from memory.

More Related