c file i/o

26
File Management in C (Topic 7: Files GTU Syllabus 2009) Created By: Vinod Pillai. [email protected] http://vinodthebest.wordpress.com Chaudhari Technical Institute MCA (506) Gandhinagar

Upload: vinod-pillai

Post on 07-Nov-2014

1.598 views

Category:

Education


3 download

DESCRIPTION

File Operation in C Language.

TRANSCRIPT

  • 1. File Management in C (Topic 7: Files GTU Syllabus 2009) Chaudhari Technical Institute MCA (506) GandhinagarCreated By: Vinod [email protected]://vinodthebest.wordpress.com
  • 2. File Management We have been using the functions such as printf and scanf to read and write data. This works fine as long as data is small. But it has two major problems: It becomes cumbersome and time consuming to handle large volume of data through terminals. The entire data is lost when either the program is terminated or the computer is turned off. Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 2
  • 3. File Management So it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying data. This concept is called files. Files: A file is a place on the disk where a group of related data is stored. FILE is a structure declared in stdio.h . We have to use file pointer, a pointer variable that points to a structure FILE. Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 3
  • 4. C File Management C supports a number o functions that have the ability to perform basic file operations: Creating a file Opening a file Reading data from a file Writing data to a file Closing a file Renaming a file Deleting a file Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 4
  • 5. Using Files in CDeclaration of File Pointer Checking the result of fopen()FILE *fp;FILE *fp1; if(fp == NULL) {Opening a File printf(Cant open); exit(1);fp=fopen(one.txt,r); } Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 5
  • 6. Using Files in CClosing Files fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 6
  • 7. File Opening Modesr= Open file for Reading r+ = Open file forw= Create file for Writing read/writea= Append text file w+ = Create file for read/write a+ = Append text file for read/writerb = Open binary file for r+b = Open binary file for Reading read/writewb= Create binary file w+b= Create binary file for for Writing read/writeab = Append binary file a+b= Append binary file for read/write Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 7
  • 8. Working with Text filesFour functions for read Four functions for writefiles from the disk: files into the disk:fscanf() fprintf()fgets() fputs()fgetc() fputc()fread() fwrite() Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 8
  • 9. Write Character to a file (Pg. 583)Char name[30]; While(name*i+!=0)int i=0; { putc(name[i],fp);FILE *fp; i++;fp=fopen(one.txt,w); }Printf(Enter Name:); fclose(fp);Scanf(%*^n+,name); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 9
  • 10. Reading Character from file (Pg. 584)int ch; //IMP While(ch!=EOF) {FILE *fp; putchar(ch); //IMPfp=fopen(one.txt,r); ch=getc(fp); }ch=getc(fp); fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 10
  • 11. Reading Character from file (Pg. 584)int ch; //IMP While(ch!=EOF) {FILE *fp; putchar(ch); //IMPfp=fopen(one.txt,r); ch=getc(fp); }ch=getc(fp); fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 11
  • 12. ProgramsExample 4 : Count the int ch1,ch2; number of characters FILE *fp1,*fp2; and number of lines. (Pg. 586) fp1=fopen(one.txt,r); fp2=fopen(two.txt,r);Example 6: Compare two files. (Pg. 588) ch1=getc(fp1); ch2=getc(fp2); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 12
  • 13. Programswhile(ch1!=EOF && if(ch1 == ch2) ch2!=EOF && { ch1==ch2) printf(Same);{ } else if(ch1 != ch2) ch1=getc(fp1); { ch2=getc(fp2); printf(Not Same);} } fclose(fp1); fclose(fp2); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 13
  • 14. ProgramsExample 7: Copy one file FILE *fp1; to another. (Pg. 589) fp1=fopen(one.txt,w);fprintf() : Writing to a file fprintf(fp1,%s, Hello);fscanf() : Reading content fprintf(fp1,%d,10); from file fclose(fp1); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 14
  • 15. ProgramsFILE *fp1; Example 13: feof(). (Pg. 595)char name[30]; char value[30];int value; FILE *fp; fp=fopen(one.txt,r);fp1=fopen(one.txt,r); while(!feof(fp))fscanf(fp1,%s, name); {fscanf(fp1,%d,value); fgets(value,30,fp); }fclose(fp1); fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 15
  • 16. Binary FilesFILE *fp1,*fp2; if(!feof(fp1))fp1=fopen(one.txt,rb); { fputc(ch,fp2);fp2=fopen(two.txt,wb); } elseint ch; { break;While(1) } }{ fclose(fp1); ch=fgetc(fp1); fclose(fp2); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 16
  • 17. Direct File Input & Outputfread() fread() & fwrite() = Tofwrite() read and write block of data.Feof() End of file =====================Ferror() - Error has void main() occurred. { FILE *fp1;(597 598) IMP int i, arr1[10], arr2[10]; Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 17
  • 18. Direct File Input & OutputFor(i=0;i