Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply



Published on

Published in: Technology

1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. FILE HANDLING IN ‘C’ Disk I/O functions High level Low level Text BinaryFormatted Un formatted formatted Un formatted
  • 2. This works fine as long as the data is small.However, many real life problems involve large volumes of data and in such situations, the console oriented I/O operations pose two major problems.1. It becomes cumbersome and time consuming to handle large volumes of data through terminals.2. The entire data is lost when either the program is terminated or computer is turned off.o It is therefore necessary to have a more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data.o The method employs the concept of files to store data. A file is place on the disk where a group of related data is stored.
  • 3. Like most other languages, c supports a number of functionsthat have the ability to perform basic file operations, whichinclude.  naming a file  opening a file  reading data from a file  writing data to a file and  closing a file.There are two distinct ways to perform file operations. --The first one is known as the low level I/O and usesUNIX system calls. --The second method is referred to as the high leveloperation and uses functions in c standard I/O library.
  • 4. HIGH LEVEL I/O FUNCTIONSfopen ()  creates a new file for use. Opens an existing file for use.fclose()  closes a file which has been opened for use.getc()  reads a character from a file .putc()  writes a character to a file.fprintf()  writes a set of data values to a file.fscanf()  reads a set of data value from a file.getw()  reads an integer from a file.
  • 5. putw()  writes an integer to a file. fseek()  sets the position to a desired point in the file. ftell()  gives the current position in file. rewind()  sets the position to beginning of the file. DEFINING AND OPENING A FILE:If we want to store data in a file in the secondary memory, we mustspecify certain thing about a file to operating system they are1.File name 2. data structure 3. purposeFor file name general format of declaring and opening a file is FILE *fp;  specifies the file name.fp= fopen (“ Filename”, “mode”);  specifies the purpose of file.
  • 6. The mode does this job. Mode can be one of the followingr: opens the text file for reading only.w: opens the text file for writing only.a : open the file for appending data to it.r+: the existing file is opened for both reading and writing. w+ : this mode allow you to open a file as text file forreading as well as writing a data to a file.a+ : this mode allows you to open a file as a text file forboth reading and writing to a file. One other hand we need to add ‘b’ for binary file i.e., rb,wb, rb+, wb+, a, ab+, ab.
  • 7. rb: this mode allows to open a file as a binary file for readingdata from it.wb: this mode allows to open a file as a binary file for writingdata to it.ab: this mode allows to open a file as a binary file forappending data at the end of the file.rb+: this mode allows to open a file as a binary file for readingas well as writing data to a file.wb+ : this mode allows to open a file as a binary file for writingdata to a file.ab+: this mode allows to open a file as a binary file for bothreading and writing data to a file.
  • 8. Example: FILE *P1, *P2; P1= fopen (“data.c”, “r”); P2= fopen (“result.c”, “w”);CLOSING A FILE: A file must be closed as soon as all operations on ithave been completed. This ensures that all outstandinginformation associated with the file is flushed out from thebuffer and all links to a file are broken.Syn: fclose ( file pointer); fclose (P1);
  • 9. getc() and putc():The getc() is an input function is used to read a singlecharacter from a file.Syntax : charvar=getc (file pointer); ch=getc (fp);The putc() is an output function used to write a singlecharacter into a file.Syntax : putc (charvar, file pointer); putc (ch, fp);
  • 10. Program to count character, spaces, tabs, and numbers in a file.#include<stdio.h>Void main(){ char ch; int nol=0, not=0, nos=0, noc=0; fp=fopen(“pri.c”, “r”); while(1) { ch=getc (fp); if ( ch== EOF) break; noc++;
  • 11. if ( ch==‘ ‘) nos++; if (ch== ‘t’) not++; If ( ch==‘n’) nol++;}fclose (fp);printf (“ number of characters = %d”, noc);printf (“ number of blanks= %d n”, nos);printf (“ number of tabs=%d n” , not);
  • 12. printf (“ number of lines = %d n”, nol);getch();}OUTPUT: number of characters =125 number of blanks =25 number of tabs =13 number of lines =22
  • 13. putw() and getw(): The putw() function is an output function and is usedto write an integer into a file.Syntax: putw( integer, file pointer) ex: putw (x,pf);The getw() function is an input function and is used to readan integer from a file.Syntax: integervar =getw( file pointer); ex: x= getw(fp);
  • 14. Program for demonstrating putw() and getw() function#include<stdio.h>#include<stdlib.h>Void main(){ FILE *f; int word1, word2; fp=fopen ( “ number.c”, “w”); if(fp==NULL) { printf (“ error opening file”);
  • 15. exit(1);} word1=94;putw(word1,fp);if ( ferror(fp))printf(“ error writing to file n”);elseprintf( “ sucessful write n” );fclose(fp);/* reopen the file */fp= fopen( “number.c”, “r”);
  • 16. if ( fp== NULL){printf(“ error opening file “);exit(1);}/*extract the word */Word =getw (fp);if (ferror (fp))printf(“ error reading file n”);elseprintf(“ successful read : word=%d “, word2);
  • 17. /* clean up*/fclose (fp);return 0;} word1 Successfully Number file 94 wrote 94 word2 94 Successful read getw pf f()
  • 18. fprintf() and fscanf() functions: The fprintf() and fscanf() functions group of mixed datasimultaneously.Syntax: fprintf( fp, “ control string “, list); ex: fprintf( %s %d %f “, name, age, 7.5);Syntax: fscanf(fp, “control string “, list); ex: fscanf(“ %s %d”, item, &quantity);Demonstrating frpintf () and fscanf() functions:#include<stdio.h>#include<conio.h>Void main()
  • 19. { FILE *fp; int rno; char name[30]; float marks; fp=fopen(“ student.c”, “w”); printf( “ enter student record n”); scanf(“ %d %s %f “,& rno, name, &marks); fprintf( “ fp, “ %d %s %f “, rno, name, marks); fclose( fp); fp= fopen(“ student.c”, “r”);
  • 20. printf(“ the student record n”);fscanf (fp, “%d %s %f”, &rno, name, &marks);printf(“ %d %s %f”, rno, name, marks);fclose (fp);} scanf() H.D Enter students fprintf() student rno name marks 1 ravi 1.Ravi 30.5 30.5 1. Ravi 30.5 1 ravi 30.5 fscanf() prinf()
  • 21. RANDOM ACCESS TO FILES:Sometimes it is required to access only a particular partof the file and not the complete file.This can be accomplished by using following function. fseek(): it is a file function. It positions file pointer on thestream. We can pass three arguments through thisfunction.The general format of fseek function is as follows: fseek( file pointer, offset, position);This function is used to move the file position to a desiredlocation within the file.
  • 22. 1. Fileptr is a pointer to the file concerned.2. Offset is a number or variable type long.3. Position in an integer number. Offset specifies the number of positions (bytes) to be moved from the location specified at position. integer Constant Location in file value 0 SEEK_SET Beginning of file 1 SEEK_CUR Current position of file 2 SEEK_END End of file Ex: fseek(fp,10,0) or fseek(fp, 10,SEEK_SET) filepointer is repositioned in forward direction by 10 bytes
  • 23.  Program to read the text file containing somesentence. Using fseek() and read the text after skipping‘n’ character from beginning of file.Void main(){FILE *fp;int n, ch;clrscr();fp=fopen( “ text.txt”, “r”);printf( “n contents of file n”);while ((ch=fgetc(fp))!=EOF)printf(“ %c”, ch);
  • 24. printf( “ n how many characters including spaceswould you like to skip?”);scanf(“%d”, &n);fseek ( fp, n, SEEK_SET)printf(“n information after %d byte n”,n);while (( ch=fgetc(fp)) !=EOF)printf(“%c”, ch);fclose(fp);}