C File I/O

3,955 views

Published on

File Operation in C Language.

Published in: Education, Technology
2 Comments
9 Likes
Statistics
Notes
No Downloads
Views
Total views
3,955
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
352
Comments
2
Likes
9
Embeds 0
No embeds

No notes for slide

C File I/O

  1. 1. File Management in C (Topic 7: Files GTU Syllabus – 2009) Chaudhari Technical Institute MCA (506) GandhinagarCreated By: Vinod Pillai.vinodvpillai@hotmail.comhttp://vinodthebest.wordpress.com
  2. 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. 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. 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. 5. Using Files in CDeclaration of File Pointer Checking the result of fopen()FILE *fp;FILE *fp1; if(fp == NULL) {Opening a File printf(“Can’t open”); exit(1);fp=fopen(“one.txt”,”r”); } Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 5
  6. 6. Using Files in CClosing Files fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 6
  7. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 18. Direct File Input & OutputFor(i=0;i<10;i++) fclose(fp1);{ fread(arr2,sizeof(int),10,fp) arr1[i]=i * 2;} fclose(fp1);fp1=fopen(“one.txt”,”wb); for(i=0;i<10;i++) {fwrite(arr,sizeof(int),10,fp1) printf(“%d”,arr2*i]); } Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 18
  19. 19. IMP Programs• Example 16. (fprintf & fscanf)• Example 17. (fread & fwrite)• Example 18. Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 19
  20. 20. Storing Structure in FileStruct item struct item t1,t2;{ File *fp; int code; fp=fopen(“one.txt”,”wb); double price; t1.code=10;}; t1.price=10.56; fwrite(&t1,sizeof(t1),1,fp);Void main(){ fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 20
  21. 21. Storing Structure in Filefp=fopen(“one.txt”,”rb);Fread(&t2,sizeof(t2),1,fp);fclose(fp); Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 21
  22. 22. Random Access of Filesfseek() = To Jump to particular Location in a Fileftell() = Current Location in a Filerewind() = Back to First Location in a File Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 22
  23. 23. Random Access of Files (fseek())int fseek(FILE *FP, long offset, int orgin); fp 0 = Successfully Starting Point: Non-Zero = Error SEEK_SET ( 0 – Beginning File) SEEK_CUR (1- Current File) Distance position indicator SEEK_END (2 – End of file) to move in bytes Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 23
  24. 24. Rewind() & ftell()IMP Example 19 (Pg. 614)Void rewind(fp);Long ftell(fp);value=ftell(fp);IMP Example 20 (Pg. 615) Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 24
  25. 25. Renaming & Deleting FileDeleting a File: Renaming a File:int remove(filename); int rename(old_name,new _name);0 = Success 1 = Failure 0 = Success 1 = Failure Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 25
  26. 26. References• Programming in ANSI C Edition 2.1 by E Balagurusamy.• Programming in C by Paradip Dey & Manas Ghosh. Prepared By: Vinod Pillai Chaudhari Technical Institute - Gandhinagar 26

×