Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 27: FILE HANDLING in C Language FILE HANDLING in C Language-2 Lecture 27.

Similar presentations


Presentation on theme: "Lecture 27: FILE HANDLING in C Language FILE HANDLING in C Language-2 Lecture 27."— Presentation transcript:

1 Lecture 27: FILE HANDLING in C Language FILE HANDLING in C Language-2 Lecture 27

2 Lecture 27: FILE HANDLING in C Language FILE pointer in C The statement: FILE *fptr1, *fptr2 ; fptr1fptr2 FILE declares that fptr1 and fptr2 are pointer type variables of type FILE. They can contain the address of a file descriptors.

3 Lecture 27: FILE HANDLING in C Language Opening FILE The statement: FILE *fptr1; fptr1 = fopen( d:\\mydata.txt", "r"); d:\\mydata.txt would open the file d:\\mydata.txt for input (reading). The statement: FILE *fptr2; fptr2 = fopen(d:\\results.txt", "w"); d:\\results.txt would open the file d:\\results.txt for output (writing).

4 Lecture 27: FILE HANDLING in C Language Opening Text Files The statement: FILE *fptr2; fptr2 = fopen (d:\\results.txt",r+"); d:\\results.txt would open the file d:\\results.txt for both reading and writing. Once the FILE is open, it stay open until you close it or end of the program reaches (which will close all files.)

5 Lecture 27: FILE HANDLING in C Language Testing for Successful Open fopenIf the FILE was not able to be opened, then the value returned by the fopen routine is NULL. d:\\mydata.txtFor example, let's assume that the file d:\\mydata.txt does not exist. Then: FILE *fptr1; fptr1 = fopen ( d:\\mydata.txt", W") ; if (fptr1 == NULL) cout<< "File 'mydata' cant be open; else cout << File opens successfully;

6 Lecture 27: FILE HANDLING in C Language Writing in FILE We can write any contents (data) in the FILE using the following FILING function: fprintf (paramter1, paramter2, paramter3); FILE pointerSignature of the variable Actual name of the variable

7 Lecture 27: FILE HANDLING in C Language Writing in FILE (Example 1) fprintf 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 Lecture 27: FILE HANDLING in C Language Writing in FILE (Example 2) fprintf 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 Lecture 27: FILE HANDLING in C Language Writing in FILE (Example 3) fprintf The fprintf function will write value from the number1, floatData and myCharacter (memory location) to hard disk location (d:\\mydata.txt). 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); }

10 Lecture 27: FILE HANDLING in C Language Writing in FILE (Example 4) fprintf The fprintf function will write value from the string 1 (memory location) to hard disk location (d:\\mydata.txt). 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); }

11 Lecture 27: FILE HANDLING in C Language 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 from beginning of FILE. void main (void) { FILE *fptr; fptr = fopen( d:\\myfile.dat, w ); // write mod int data = 353; fprintf (fptr,%d, data); }

12 Lecture 27: FILE HANDLING in C Language Solution Open in Append MOD a+ or w+ In append MOD, the previous data in the FILE 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 Lecture 27: FILE HANDLING in C Language OUTLINE How we can read data/information from the FILES. How we can close the FILES.

14 Lecture 27: FILE HANDLING in C Language 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 Lecture 27: FILE HANDLING in C Language Reading from FILE (Example 1) fscanf 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 Lecture 27: FILE HANDLING in C Language 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 Lecture 27: FILE HANDLING in C Language 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 Lecture 27: FILE HANDLING in C Language 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 Lecture 27: FILE HANDLING in C Language 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); } void main (void) { FILE *fptr; fptr = fopen( d:\\myfile.dat, r ); // read mod char character; while (1) { fscanf (fptr,%c,&character); cout << character; } write.cpp read.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.

20 Lecture 27: FILE HANDLING in C Language End of file The end-of-file indicator informs the program when there are no more data (no more bytes) to be processed. fscanfThere are a number of ways to test for the end- of-file condition. One way is to use the value returned by the fscanf function: int istatus; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == EOF ) { cout << End-of-file encountered.; }

21 Lecture 27: FILE HANDLING in C Language Reading byte by byte Information from FILE (Example 6) 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; } read.cpp

22 Lecture 27: FILE HANDLING in C Language Closing FILES The statements: fclose ( fptr1 ) ; fclose ( fptr2 ) ; will close the files and release the file descriptor space from memory.


Download ppt "Lecture 27: FILE HANDLING in C Language FILE HANDLING in C Language-2 Lecture 27."

Similar presentations


Ads by Google