File handling in c language

1,532 views

Published on

File handling in c language

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

No Downloads
Views
Total views
1,532
On SlideShare
0
From Embeds
0
Number of Embeds
653
Actions
Shares
0
Downloads
166
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

File handling in c language

  1. 1. File Handling in ‘C’ Presented By Harish Gyanani
  2. 2. WHAT IS FILE? File is named location of stream of bits. It may be stored at singe place or different places but it represents a single stream.
  3. 3. What is stream in c programming language? Stream is not a hardware it is linear queue which connect file to program and passes block of data in both direction .So it is independent of devices which we are using. We can also define stream as source of data. This source can be (a) A file (b) Hard disk or CD, DVD etc. (c) I/O devices etc. In c programming language there are two type of stream. (a) Text streams (b) Binary streams
  4. 4. What is buffer in c programming language? Buffer is a technique which reduces number of I/O call.
  5. 5. Create a File #include<stdio.h> main() { FILE *p; p=fopen(“abcd.txt","w"); if(p) { printf("created"); } else { printf("not created"); } fclose(p); }
  6. 6. Write in a Blank File #include<stdio.h> main() { FILE *p; char a; p=fopen(“file1.txt","w"); if(p) { printf("openedn"); fputs("hello i am a student",p); } else { printf("not openedn"); } fclose(p); }
  7. 7. Open a file in default program //we can open any type of file //Example - ppt,txt,jpg,mp3 etc. #include<stdio.h> main() { system(“D:abcdxyz.mp3"); } Above code will play xyz.mp3 file in Windows Media Player(default program) located in D drive ->abcd folder
  8. 8. Create, Write and Read A File Altogether.1 //THIS IS A PROGRAM TO DEMONSTRATE CREATE, WRITE AND READ A FILE ALTOGETHER #include<stdio.h> main() { FILE *y; char t; //creating y=fopen("yashuu.txt","w"); if(y) { printf("createdn"); } else { printf("not createdn"); } //writing fputs("this is yashu",y); fcolse(y);
  9. 9. Create, Write and Read A File Altogether.2 //reading y=fopen("yashuu.txt","r"); if(y) { printf("openedn"); } else { printf("not openedn"); } for(;(t=fgetc(y))!=EOF;) { //fgetc is used to read a character from file printf("%c",t); } fclose(y); }
  10. 10. Count characters in a file #include<stdio.h> main() { FILE *y; int count; char ch; fopen("hh.txt","r"); for(count=0;(ch=fgetc(y))!=EOF;count++) {} printf("%d",count); fclose(y); }
  11. 11. Count lines in a File #include<stdio.h> main() { FILE *y; int count=0; char ch; y=fopen("yy.txt","r"); while((ch=fgetc(y))!=EOF) { if(ch=='n') { count++; } } count++; printf("%d",count); fclose(y); }
  12. 12. Count words of a File #include<stdio.h> main() { int count=0; char ch; FILE *y; y=fopen("fight.txt","r"); while((ch=fgetc(y))!=EOF) { if(ch==32) { count++; } } count++; printf("%d",count); fclose(y); }
  13. 13. Append a File #include<stdio.h> main() { FILE *p; char a; p=fopen("yashu65.txt","a"); if(p) { printf("openedn"); fputs("hello i m yashun",p); } else { printf("not openedn"); } fclose(p); }
  14. 14. Introduction to fseek().1 Sets the position indicator associated with the stream to a new position.
  15. 15. Introduction to fseek().2 Parameters • stream -- This is the pointer to a FILE object that identifies the stream. • offset -- This is the number of bytes to offset from whence. • whence -- This is the position from where offset is added. It is specified by one of the following constants:
  16. 16. Introduction to fseek().3 Constant Description SEEK_SET Beginning of file SEEK_CUR Current position of the file pointer SEEK_END End of file
  17. 17. Introduction to fseek().4 Return Value This function returns zero if successful, else it returns nonzero value.
  18. 18. fseek() to read from 6th position #include <stdio.h> main () { FILE *fp; char c; fp = fopen("file.txt","r"); fseek( fp, 5, SEEK_SET ); while((c=fgetc(fp))!=EOF) { printf("%c",c); } fclose(fp); }
  19. 19. fseek() example of overwriting #include <stdio.h> main () { FILE *fp; fp = fopen("file.txt","w"); fputs("This is krish", fp); fseek( fp, 2, SEEK_SET ); fputs("at", fp); fclose(fp); }
  20. 20. fseek() Example of SEEK_SET.1 The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz” SEEK_SET is used to count characters from the beginning #include<stdio.h> #include<string.h> main() { FILE *y=fopen("abc.txt","r"); char b; int i; //skipping 5 characters using this loop for(i=0;i<5;i++) { b=fgetc(y); }
  21. 21. fseek() Example of SEEK_SET.2 h fseek(y,7,SEEK_SET); b=fgetc(y); printf("%c",b); } The output of this program is – Because it skips 7 characters from beginning and 8th character of this file is ‘h’
  22. 22. fseek() Example of SEEK_CUR.1 • The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz” • SEEK_CUR is used to count characters from the currect position of file pointer • #include<stdio.h> • #include<string.h> • main() • { • FILE *y=fopen("abc.txt","r"); • char b; • int i; • //skipping 5 characters using this loop • for(i=0;i<5;i++) • { • b=fgetc(y); • }
  23. 23. fseek() Example of SEEK_CUR.2 fseek(y,7,SEEK_CUR); b=fgetc(y); printf("%c",b); } The output of this program is – Because it skips 7 characters from current position which is 5; means 5+7 = 12 chars skipped. The 13th character of this file is ‘m’. m
  24. 24. fseek() Example of SEEK_END The below example use ‘abc.txt’ file which contains “abcdefghijklmnopqrstuvwxyz” SEEK_END set file pointer to the last character position #include<stdio.h> #include<string.h> main() { FILE *y=fopen("abc.txt","r"); char b; fseek(y,-10,SEEK_END); b=fgetc(y); printf("%c",b); } The output of this program is – q -10 means 10th positioned character from the last which is ‘q’.
  25. 25. Example of ftell() #include <stdio.h> main () { FILE *fp; char c; fp = fopen("file.txt","r"); fseek( fp, 5, SEEK_SET ); while((c=fgetc(fp))!=EOF) { printf("%c - position : %dn",c,ftell(fp)); } fclose(fp); }
  26. 26. a+ mode • FILE *f1=fopen("test.dat","a+"); • Open for reading and appending (writing at end of file). The file is created if it does not exist. • The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. • There is just one pointer which initially is at the start of the file but when a write operation is attempted it is moved to the end of the file.
  27. 27. r+ mode • read/update: Open a file for update (both for input and output). The file must exist.
  28. 28. w+ mode • write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
  29. 29. What is difference between file opening mode r+ and w+? Both r+ and w+ we can read ,write on file but r+ does not truncate (delete) the content of file as well it doesn’t create a new file if such file doesn’t exits while in w+ truncate the content of file as well as create a new file if such file doesn’t exists.
  30. 30. Read integers From File #include<stdio.h> main() { FILE *p; int sum=0,number; p=fopen("kl.txt","r"); if(p==NULL) { printf("file not opened "); } else { while(!feof(p)) { fscanf(p,"%d",&number); printf("%dn",number); sum=sum+number; } printf("%d",sum); fclose(p); } }
  31. 31. Rename a File #include <stdio.h> main() { int result; char oldname[] ="rt.txt"; char newname[] ="network.txt"; result= rename( oldname , newname ); if ( result == 0 ) { puts ( "File successfully renamed" ); } else { perror( "Error renaming file" ); } }
  32. 32. Delete a File #include <stdio.h> main () { if( remove( "debug.log" ) != 0 ) { perror( "Error deleting file" ); } else { puts( "File successfully deleted" ); } }
  33. 33. Make a Folder/Directory #include<stdio.h> main() { //when folder is created it returns false, otherwise true int a=system("mkdir d:vikasabcd"); if(a) { printf("folder not created"); } else { printf("folder created"); } }
  34. 34. Rename a Folder #include <stdio.h> main() { int result; char oldname[] ="faltu"; char newname[] ="faltu2"; result= rename( oldname , newname ); if ( result == 0 ) { puts ( "Folder successfully renamed" ); } else { perror( "Error renaming Folder" ); } }
  35. 35. Scan all files and folders inside a Directory/Folder #include<stdio.h> main() { system("dir d:fca"); }
  36. 36. Delete a Folder #include <stdio.h> main () { if( system("rmdir rt") ) { perror( "Error deleting folder" ); } else { puts( "Folder successfully deleted" ); } }

×