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

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

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

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

Lecture 27: FILE HANDLING in C Language

FILE HANDLING in C Language-2

Lecture 27

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

Lecture 27: FILE HANDLING in C Language

FILE pointer in C

• The statement:

FILE *fptr1, *fptr2 ;

declares that fptr1fptr1 and fptr2fptr2 are pointer type variables of type FILEFILE. They can contain the address of a file descriptors.

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

Lecture 27: FILE HANDLING in C Language

Opening FILE• The statement: FILE *fptr1;fptr1 = fopen( “d:\\mydata.txt", "r");

would open the file d:\\mydata.txtd:\\mydata.txt for input (reading).

• The statement: FILE *fptr2; fptr2 = fopen(“d:\\results.txt", "w"); would open the file d:\\d:\\results.txtresults.txt for output (writing).

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

Lecture 27: FILE HANDLING in C Language

Opening Text Files

• The statement: FILE *fptr2; fptr2 = fopen (“d:\\results.txt",“r+");

would open the file d:\\d:\\results.txtresults.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.)

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

Lecture 27: FILE HANDLING in C Language

Testing for Successful Open

• If the FILE was not able to be opened, then the value returned by the fopenfopen routine is NULL.

• For example, let's assume that the file d:\\d:\\mmydata.txtydata.txt does 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”;

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

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 pointer

Signature of the variable

Actual name of the variable

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

Lecture 27: FILE HANDLING in C Language

Writing in FILE (Example 1)

The fprintffprintf 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);

}

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

Lecture 27: FILE HANDLING in C Language

Writing in FILE (Example 2)

The fprintffprintf 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);

}

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

Lecture 27: FILE HANDLING in C Language

Writing in FILE (Example 3)

The fprintffprintf 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);

}

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

Lecture 27: FILE HANDLING in C Language

Writing in FILE (Example 4)

The fprintffprintf 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);

}

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

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

}

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

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

}

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

Lecture 27: FILE HANDLING in C Language

OUTLINE

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

• How we can close the FILES.

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

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

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

Lecture 27: FILE HANDLING in C Language

Reading from FILE (Example 1)

The fscanffscanf 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);}

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

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

}

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

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

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

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;

}

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

Lecture 27: FILE HANDLING in C LanguageReading 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.

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

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.

• There are a number of ways to test for the end-of-file condition. One way is to use the value returned by the fscanffscanf function:

int istatus;istatus = fscanf (fptr1, "%d", &var) ;if ( istatus == EOF ){ cout << “End-of-file

encountered.”;}

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

Lecture 27: FILE HANDLING in C LanguageReading 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

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

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.