files in c++

1,030 views
822 views

Published on

Courtesy: Sir Nabeel Sabir's Share Folder

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,030
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

files in c++

  1. 1. Chapter 12: Files
  2. 2. ObjectivesIn this chapter, you will learn: How to declare a file pointer How to open file, check whether the file exist or not How to read, write and append to the file How to close the file How to read from a file and write to the other file How to read from a file and write to the terminal screen vice versa01/07/13 CCSB164: C Programming 2
  3. 3. File Program Flow 1. Declare file pointer 2. Open the file 3. Set the operation mode (read, write, append) 4. Check if the file exist 5. Do the operation (fprintf, fscanf, fgetc, fgets, fputc) 6. Close the file01/07/13 CCSB164: C Programming 3
  4. 4. Declare a file pointer Why? When? How?  Eg: FILE *thefile; FILE *Pptr; FILE *fPointer; Question: If we didn’t declare file pointer, what will happen?01/07/13 CCSB164: C Programming 4
  5. 5. Open file & set operation mode Why? When? How?  Eg: fopen("myfile.txt","w"); fopen(“power.txt",“r"); fopen(“test.txt",“a+"); Question: How to read, write and append to file if we didn’t open the file?01/07/13 CCSB164: C Programming 5
  6. 6. Check if the file exist Why? When? How?  Eg: if (thefile == NULL) if (Pptr == NULL) if (fPointer == NULL) Question: If the file didn’t exist, can we read the file content?01/07/13 CCSB164: C Programming 6
  7. 7. File operation Why? When? How?  Eg: fputc(a, thefile); fgetc(thefile); fscanf(thefile,"%s",name); fprintf(thefile, "my %s, %s is %d years old",relationship,name,age); fgets(occupation,30, thefile); Question: What is the difference between these???01/07/13 CCSB164: C Programming 7
  8. 8. Close the file Why? When? How?  Eg: fclose(thefile); fclose(Pptr); fclose(fPointer); Question: What will happen if we didn’t close the file?01/07/13 CCSB164: C Programming 8
  9. 9. Learning from Example Why? When? How?  REMEMBER: learning from the examples, NOT memorizing examples!!! Question: Don’t understand? Study and Think before you Ask!01/07/13 CCSB164: C Programming 9
  10. 10. Example 1  This program will write character ‘a’ four times in a fileint i, total = 4;char ch = a;FILE *thefile; /*initializing a file pointer */thefile = fopen("myfile.txt","w"); /* open the file for writing */if (thefile == NULL) /* test whether the open operation is successful or not */ { printf("File fail to openn"); }else{ for (i = 1; i <= total; i++) { fputc(a, thefile); fputc(n,thefile); }} 01/07/13 CCSB164: C Programming 10
  11. 11. Output This program will write character ‘a’ four times in a file01/07/13 CCSB164: C Programming 11
  12. 12. Example 2  This program will read characters one by one from the existing char_test.txt using loopint i, total = 8;char ch;FILE *thefile;if ((thefile = fopen("char_test.txt","r")) == NULL){ printf("File fail to openn");}else{ for (i = 1; i <= total; i++) { ch = fgetc(thefile); // read a character from the file printf("%c",ch); // print the value of ch to the screen }} 01/07/13 CCSB164: C Programming 12
  13. 13. Output This program will read characters one by one from the existing char_test.txt using loop01/07/13 CCSB164: C Programming 13
  14. 14. Example 2  An improved version to print the file content to the terminal by checking whether it is EOFint i, total = 8;char ch;FILE *thefile;if ((thefile = fopen("char_test.txt","r")) == NULL){ printf("File fail to openn");}else{ while ((ch = fgetc(thefile)) != EOF) /*we test whether the read character is the EOF char */ { printf("%c",ch); // print the value of ch to the screen }} 01/07/13 CCSB164: C Programming 14
  15. 15. Example 3  This program will read the content of an existing file character by character, print the characters to the screen and count the number of characters in the fileFILE *myfile;char ch;int count = 0;if ((myfile = fopen("mytext.txt","r")) == NULL){ printf("File fail to opennn");}else{ while( (ch = fgetc(myfile)) != EOF) { putchar(ch); count++; }printf("nnThere are %d characters in this filenn",count);} fclose(myfile); 01/07/13 CCSB164: C Programming 15
  16. 16. Output This program will read the content of an existing file character by character, print the characters to the screen and count the number of characters in the file01/07/13 CCSB164: C Programming 16
  17. 17. Example 4  This program will read the content of a file using fscanf( )FILE *mysibling;char name[20];if ((mysibling = fopen("sibling.txt","r")) == NULL){ printf("file fail to openn");}else{while (!feof(mysibling)) // checking whether it is the end of file or not{ fscanf(mysibling,"%s",name); printf("%sn",name);}}fclose(mysibling); 01/07/13 CCSB164: C Programming 17
  18. 18. Output This program will read the content of a file using fscanf( )01/07/13 CCSB164: C Programming 18
  19. 19. Example 5 This program will ask the user to enter a name, relationship between the user and the person whose name is entered, and the age of the named person. The program will then write this information to a fileFILE *myfamily;char name[20], relationship[20];int age;if ((myfamily = fopen("myfamily.txt","w")) == NULL){ printf("file fail to openn");}elseprintf("Enter the name: ");gets(name); // reading from the keyboardprintf("enter the relationship: ");gets(relationship); // reading from the keyboard{01/07/13 CCSB164: C Programming 19
  20. 20. Example 5printf("enter the age of that person: ");scanf("%d", &age); // reading from the keyboard// writing the info into a file in certain fromatfprintf(myfamily, "my %s, %s is %d years old",relationship,name,age);}fclose(myfamily); Output 01/07/13 CCSB164: C Programming 20
  21. 21. Example 6 This program will use fscanf to read from an existing file. fgets is used to read more than one word in one goFILE *mysibling;char name[20], occupation[30];int age;if ((mysibling = fopen("sibling_age.txt","r")) == NULL){printf("file fail to openn");}else{while (!feof(mysibling)){// reading from the filefscanf(mysibling,"%s%d",name,&age); // reading the name and age01/07/13 CCSB164: C Programming 21
  22. 22. Example 6 This program will use fscanf to read from an existing file. fgets is used to read more than one word in one gofgets(occupation,30,mysibling); // reading the occupation// since some occupation have more than one word e.g. assistant manager, we use fgets() instead of fscanf()printf("%s %d %s n",name,age, occupation); // print to the screen}}fclose(mysibling); Output01/07/13 CCSB164: C Programming 22
  23. 23. Example 7  This program will read from a source file and write to a destination fileFILE *fin, *fout;char readfile[20];char writefile[20];char getchar;printf("Enter the source file name: ");gets(readfile);printf("Enter the destination file name: ");gets(writefile);fin = fopen(readfile,"r");fout = fopen(writefile,"w");if(fin==NULL)printf("File could not be found"); 01/07/13 CCSB164: C Programming 23
  24. 24. Example 6  This program will read from a source file and write to a destination fileelse{while((getchar=fgetc(fin)) !=EOF){ /*read character by character from the source file*/ fputc(getchar,fout); /*write character by character to the destination file*/} fclose(fin); fclose(fout);} 01/07/13 CCSB164: C Programming 24
  25. 25. Summary  Declare file pointer  Open the file  Set the operation mode (read, write, append)  Check if the file exist  Do the operation (fprintf, fscanf, fgetc, fputc)  Close the file01/07/13 CCSB164: C Programming 25

×