1 / 26

FILES

FILES. Explain files. Discuss text File and binary File. Use basic file Operations & functions. Explain file pointer. Explain Formatted/Unformatted I/O Statements in File. Discuss current active pointer. Data files Can be created, updated, and processed by C programs

Télécharger la présentation

FILES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILES

  2. Explain files • Discuss text File and binary File • Use basic file Operations & functions • Explain file pointer • Explain Formatted/Unformatted I/O Statements in File • Discuss current active pointer

  3. Data files • Can be created, updated, and processed by C programs • Are used for permanent storage of large amounts of data • Storage of data in variables and arrays is only temporary.

  4. Files • A file may be anything from a disk file to a terminal or a printer. • A file is associated with a stream by performing an open operation and is disassociated from a stream by a close operation. • All files are automatically closed when the program, using them, terminates, normally by main() returning to the operating system or by a call to exit(). • Files are not closed when a program crashes

  5. File : A file is a place on the disk where a group of related data is stored simply defined as “Collection of related information”

  6. Difference between text and binary file • A binary file is treated as raw data and read byte-by-byte. The binary file do not store any special character of end-of-line. • A text file is considered to contain lines of text that are separated by some end-of-line markings.

  7. Basic file operations • Naming a file • Opening a file • Reading data from a file • Writing data to a file • Closing a file

  8. Basic File functions

  9. Using feof() • The Syntax of this Function is : int feof(FILE *fp); • Returns true if end-of-file indicator (no more data to process) is set for the specified file

  10. File Pointer It is a pointer to a structure which contains the information about the file Syntax : FILE *file pointername; FILE *fp; For Example : Opening a File pointername = fopen(“filename”, “file mode”); Syntax : fp=fopen(“emp.txt”,”w”); For Example :

  11. Closing a File Syntax : fclose(filepointer name); For Example : fclose(fp);

  12. File I/O Operations • Unformatted file I/O functions fputc() & fgetc() – Char. Orient. file I/O Operations fputs() & fgets() – String Orient. file I/O Operations • Formatted file I/O functions fprintf() and fscanf() – mixed data oriented file I/O Operations

  13. Writing a character • The function used for writing characters to a file, that was • previously opened using fopen(), is fputc(). • Syntax : fputc(ch, FILE *fp); Reading a character • The fgetc() function is used for reading characters from a file • opened in read mode, using fopen(). fgetc(FILE *fp); • Syntax :

  14. Character Oriented Functions To create a file consisting of characters: fputc() #include<stdio.h> void main() { FILE *fp; (fp is a pointer to FILE type) char c; fp=fopen(“sample.txt”,”w”); printf(“Keep typing characters. Type ‘q’ to terminate”); c=getchar(); while(c!=‘q’) { Output: fputc(c,fp); Keep typing characters. Type ‘q’ to terminate c=getchar(); artyhefhfgfgrq } fclose(fp); }

  15. To read a file consisting of characters: fgetc() #include<stdio.h> void main() { FILE *fp; char c; fp=fopen(“sample.txt”,”r”); printf(“The contents of the file ‘sample’ are:\n”); while(!feof(fp)) { Output: c=fgetc(fp); The contents of the file ‘sample’ are: putchar(c); artyhefhfgfgrq } fclose(fp); }

  16. String Input / Output • fputs() and fgets(), which write and read character strings to and from disk file. • The Syntax for the above functions are - fputs(buffer, FILE *fp); buffer is the name of the characterarray fgets( buffer, size, FILE *fp); Size is an integer value

  17. String Oriented Functions To create a file consisting of strings: fputs() #include<stdio.h> void main() { FILE *fp; char name[20]; int i,n; fp=fopen(“names.txt”,”w”); printf(“Enter the number of names\n”); scanf(“%d”,&n); printf(“Enter %d names\n”,n); for(i=1;i<=n;i++) Output: Enter no. of names: 4 { Enter 4 names gets(name); Joel fputs(name,fp); Ida } Mary fclose(fp); Jeba }

  18. To read a file consisting of strings: fgets() #include<stdio.h> void main() { FILE *fp; char name[20]; fp=fopen(“names.txt”,”r”); printf(“Strings are: \n”); while(!feof(fp)) Output: { Strings are: fgets(name,20,fp); Joel puts(name); Ida printf(“\n); Mary } Jeba fclose(fp); }

  19. fprintf() and fscanf() • These functions are similar to printf() and scanf() except that they operate with files. • Syntax fprintf() fprintf(FILE *fp, “Control string”, arguments-list); fscanf() fscanf(FILE *fp, “control string”,argument-list);

  20. Mixed data oriented functions To Create a file consisting of employees’ details: fprintf() #include<stdio.h> struct emp { int empno; char name[20]; float salary; }e; void main() Output: { Enter the number of employees FILE *fp; 1 int i,n; Enter 1 Employees’ details fp=fopen(“emp.txt”,”w”); 123 Joel 1234 printf(“Enter the number of employees\n”); scanf(%d”,&n); printf(“Enter %d Employees’ Details\n”,n); for (i=1;i<=n;i++) { scanf(“%d%s%f”,&e.empno,e.name,&e.salary); fprintf(fp,”%d%s%f”, ”,e.empno,e.name,e.salary); } fclose(fp); }

  21. To read a file consisting of employees’ details: fscanf() #include<stdio.h> struct emp { int empno; char name[20]; float salary; }e; void main() { FILE *fp; Output: int i,n; Contents of the file emp.txt fp=fopen(“emp.txt”,”r”); 123 Joel 1234 printf(“Contents of the file emp.txt”); while(!feof(fp)) { fscanf(fp,“%d%s%f”,&e.empno,e.name,&e.salary); Printf(”%d%s%f”, ”,e.empno,e.name,e.salary); } fclose(fp); }

  22. rewind() function • The syntax for rewind() is : rewind(fp); • The prototype for the rewind() is available in stdio.h • The rewind() function resets the file position indicator to the beginning of the file.

  23. Current File Pointer Position • In order to keep track of the position where I/O operations take place a pointer is maintained in the file structure. • The current location of the current active pointer can be found with the help of the ftell() function long int ftell( FILE *fp);

  24. Setting Current File Pointer Position • The fseek() function repositions the filepointer by a specified number of bytes from the start, the current position or the end of the stream depending upon the position specified in the fseek() function. • The Syntax of the fseek() function is - int fseek( FILE *fp, long int offset, int origin); • Here the offset is the number of bytes beyond the file location given by origin.

  25. Setting Current File Pointer Position • The origin indicates the starting position of the search and must have the value of either 0 ,1 or 2.

  26. The fread() and fwrite() function • Used to read and write an entire block of data to and from a file • The Syntax for these functions are - fread() size_t fread(void *buffer, size_t num_bytes, size_t count FILE *fp); fwrite() size_t fwrite(const void *buffer, size_t num_bytes, size_t count FILE *fp);

More Related