FILE HANDLING in C Language2 Lecture 27 By

FILE HANDLING in C Language-2 Lecture 27 By - Kanchan Bala

FILE pointer in C • The statement: FILE *fptr 1, *fptr 2 ; declares that fptr 1 and fptr 2 are pointer type variables of type FILE They can contain the address of a file descriptors. By - Kanchan Bala

Opening FILE • The statement: FILE *fptr 1; fptr 1 = fopen( “d: \mydata. txt", "r"); would open the file d: \mydata. txt for input (reading). • The statement: FILE *fptr 2; fptr 2 = fopen(“d: \results. txt", "w"); would open the file d: \results. txt for output (writing). By - Kanchan Bala

Opening Text Files • The statement: FILE *fptr 2; fptr 2 = fopen (“d: \results. txt", “r+"); 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. ) By - Kanchan Bala

Testing for Successful Open • If the FILE was not able to be opened, then the value returned by the fopen routine is NULL. • For example, let's assume that the file d: \mydata. txt does not exist. Then: FILE *fptr 1; fptr 1 = fopen ( “d: \mydata. txt", “W") ; if (fptr 1 == NULL) cout<< "File 'mydata' can’t be open”; else cout << “File opens successfully”; By - Kanchan Bala

Writing in FILE • We can write any contents (data) in the FILE using the following FILING function: fprintf (paramter 1, paramter 2, paramter 3); FILE pointer Signature of the variable By - Kanchan Bala Actual name of the variable

Writing in FILE (Example 1) void main (void) { FILE *fptr; fptr = fopen( “d: \mydata. txt” , ”w” ); // write mod int number 1 = 93; fprintf (fptr, ”%d”, number 1); } The fprintf function will write value from the number 1 (memory location) to hard disk location (“d: \mydata. txt”). By - Kanchan Bala

Writing in FILE (Example 2) void main (void) { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”w” ); // write mod long number 1 = 93; float. Data = 34. 63; fprintf (fptr, ”%d%f”, number 1, float. Data); } The fprintf function will write value from the number 1 & float. Data (memory location) to hard disk location (“d: \myfile. dat”). By - Kanchan Bala

Writing in FILE (Example 3) void main (void) { FILE *fptr; fptr = fopen(“d: \myfile. dat”, ”w”); // write mod long number 1 = 93; float. Data = 34. 63; char my. Character = ‘D’; fprintf (fptr, ”%d%f%c”, number 1, float. Data, my. Character); } The fprintf function will write value from the number 1, float. Data and my. Character (memory location) to hard disk location (“d: \mydata. txt”). By - Kanchan Bala

Writing in FILE (Example 4) void main (void) { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”w” ); // write mod char string 1 [40]; strcpy (string 1, “My text information is Pakistan”); fprintf (fptr, ”%s”, string 1); } The fprintf function will write value from the string 1 (memory location) to hard disk location (“d: \mydata. txt”). By - Kanchan Bala

Writing in FILE (problem with “w” MOD) void main (void) { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”w” ); // write mod int data = 353; fprintf (fptr, ”%d”, data); } • 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. By - Kanchan Bala

Solution Open in Append MOD “a+” or “w+” void main (void) { FILE *fptr; fptr = fopen(“d: \myfile. dat”, ”a+”); // write mod int data = 353; fprintf (fptr, ”%d”, data); } • 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). By - Kanchan Bala

OUTLINE • How we can read data/information from the FILES. • How we can close the FILES. By - Kanchan Bala

Reading from FILE • We can write any contents (data) in the FILE using the following FILING function: FILE pointer return. Parameter fscanf (paramter 1, paramter 2, paramter 3); Signature of the variable End of file indicator By - Kanchan Bala Actual name of the variable

Reading from FILE (Example 1) void main (void) { FILE *fptr; fptr = fopen( “d: \mydata. txt” , ”r+” ); // random mod int number 1 = 93, number 2 = 0; fprintf (fptr, ”%d”, number 1); fscanf (fptr, ”%d”, &number 2); } The fscanf function will read value from the hard disk location (“d: \mydata. txt”) to number 1 (memory location). By - Kanchan Bala

Reading from FILE (Example 2) void main (void) { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”r+” ); // random mod long number 1 = 93, read 1 = 0; float. Data = 34. 63, read 2 = 0; fprintf (fptr, ”%d%f”, number 1, float. Data); fscanf (fptr, ”%d%f”, &read 1, &read 2); } By - Kanchan Bala

Reading from FILE (Example 3) void main (void) { FILE *fptr; fptr = fopen(“d: \myfile. dat”, ”r+”); // random mod long number 1 = 93, read 1 = 0; float. Data = 34. 63, read 2 = 0; char my. Character = ‘D’, read 3 = ‘ ‘; fprintf (fptr, ”%d%f%c”, number 1, float. Data, my. Character); fscanf (fptr, ”%d%f%c”, &read 1, &read 2, &read 3); cout << “Value are” << read 1 << read 2 << read 3; } By - Kanchan Bala

Reading from FILE (Example 4) void main (void) { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”r+” ); // random mod char string 1 [40], string 2 [40]; strcpy (string 1, “My text information is Pakistan”); fprintf (fptr, ”%s”, string 1); fscanf (fptr, ”%s”, string 2); cout << string 2; } By - Kanchan Bala

Reading byte by byte Information from FILE (Example 5) void main (void) write. cpp { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”w” ); // write mod char string 1 [40]; strcpy (string 1, “My text information is Pakistan”); fprintf (fptr, ”%s”, string 1); } The problem with this void main (void) read. cpp code is that it can read { FILE *fptr; the file data fptr = fopen( “d: \myfile. dat” , ”r” ); //out readof mod from your disk. character; while (1) { fscanf (fptr, ”%c”, &character); cout << character; } } By - Kanchan Bala Control it using ‘End of file’ marker.

End of file • The end-of-file indicator informs the program when there are no more data (no more bytes) to be processed. • There 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 (fptr 1, "%d", &var) ; if ( istatus == EOF ) { cout << “End-of-file encountered. ”; } By - Kanchan Bala

Reading byte by byte Information from FILE (Example 6) void main (void) read. cpp { FILE *fptr; fptr = fopen( “d: \myfile. dat” , ”r” ); // read mod character; while (1) { int status = 0; status = fscanf (fptr, ”%c”, &character); if ( status == EOF ) break; cout << character; } } By - Kanchan Bala

Closing FILES • The statements: fclose ( fptr 1 ) ; fclose ( fptr 2 ) ; will close the files and release the file descriptor space from memory. By - Kanchan Bala
- Slides: 22