1 / 59

Files in C Rohit Khokher

Files in C Rohit Khokher. Files in C. Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems It becomes cumbersome and time consuming to handle large volumes of data through terminals.

megara
Download Presentation

Files in C Rohit Khokher

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 in C RohitKhokher

  2. Files in C • Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems • It becomes cumbersome and time consuming to handle large volumes of data through terminals. • The entire data is lost when either the program is terminated or computer is turned off therefore it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data.

  3. File management in C File management in C, File operation functions in C, Defining and opening a file, Closing a file, The getw and putw functions, The fprintf & fscanf functions, Random access to files and fseek function.

  4. File operation functions in C:

  5. Defining and opening a file • FILE *fp; fp=fopen(“filename”,”mode”); The variable fp is a pointer to the data type FILE. The File is a structure that is defined in the I/O Library. “filename” the file named filename and assigns an identifier to the FILE type pointer fp. The mode defines the purpose R open the file for read only. W open the file for writing only. A open the file for appending data to it.

  6. Defining and opening a file FILE *p1, *p2; p1=fopen(“data”,”r”); p2=fopen(“results”,”w”); In these statements the p1 and p2 are created and assigned to open the files data and results respectively the file data is opened for reading and result is opened for writing. In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occur.

  7. Files in C • In C, each file is simply a sequential stream of bytes. C imposes no structure on a file. • A file must first be opened properly before it can be accessed for reading or writing. When a file is opened, a stream is associated with the file. • Successfully opening a file returns a pointer to (i.e., the address of) a file structure,which contains a file descriptor and a file control block.

  8. Files in C • The statement: FILE *fptr1, *fptr2 ; declares that fptr1 and fptr2are pointer variables of type FILE. They will be assigned the address of a file descriptor, that is, an area of memory that will be associated with an input or output stream. • Whenever you are to read from or write to the file, you must first open the file and assign the address of its file descriptor (or structure) to the file pointer variable.

  9. Opening Files • The statement: fptr1 = fopen ( "mydata", "r" ) ;  would open the file mydata for input (reading). • The statement: fptr2 = fopen ("results", "w" ) ;  would open the file results for output (writing). • Once the files are open, they stay open until you close them or end the program (which will close all files.)

  10. Testing for Successful Open • If the file was not able to be opened, then the value returned by the fopenroutine is NULL. • For example, let's assume that the file mydata does not exist. Then: FILE *fptr1 ; fptr1 = fopen ( "mydata", "r") ; if (fptr1 == NULL) { printf ("File 'mydata' did not open.\n") ; }

  11. Reading From Files • In the following segment of C language code: int a, b ; FILE *fptr1, *fptr2 ; fptr1 = fopen ( "mydata", "r" ) ; fscanf ( fptr1, "%d%d", &a, &b) ; the fscanf function would read values from the file "pointed" to by fptr1 and assign those values to a and b.

  12. End of File • The end-of-file indicator informs the program when there are no more data (no more bytes) to be processed. • There are a number of ways to test for the end-of-file condition. One is to use the feof function which returns a true or false condition: fscanf (fptr1, "%d", &var) ; if ( feof (fptr1) ) { printf ("End-of-file encountered.\n”); }

  13. End of File • There are a number of ways to test for the end-of-file condition. Another way is to use the value returned by the fscanf function: int istatus ; istatus = fscanf (fptr1, "%d", &var) ; if ( istatus == EOF ) { printf ("End-of-file encountered.\n”) ; }

  14. Writing To Files • Likewise in a similar way, in the following segment of C language code: int a = 5, b = 20 ; FILE *fptr2 ; fptr2 = fopen ( "results", "w" ) ; fprintf ( fptr2, "%d %d\n", a, b ) ; the fprintffunctions would write the values stored in a and b to the file "pointed" to by fptr2.

  15. Closing Files • The statements: fclose ( fptr1 ) ; fclose ( fptr2 ) ; will close the files and release the file descriptor space and I/O buffer memory.

  16. Reading and Writing Files #include <stdio.h> int main ( ) { FILE *outfile, *infile ; int b = 5, f ; float a = 13.72, c = 6.68, e, g ; outfile = fopen ("testdata", "w") ; fprintf (outfile, "%6.2f%2d%5.2f", a, b, c) ; fclose (outfile) ;

  17. Reading and Writing Files infile = fopen ("testdata", "r") ; fscanf (infile,"%f %d %f", &e, &f, &g) ; printf ("%6.2f%2d%5.2f\n", a, b, c) ; printf ("%6.2f,%2d,%5.2f\n", e, f, g) ; } 13.72 5 6.68 13.72, 5, 6.68

  18. Closing a file • The input output library supports the function to close a file; it is in the following format. • fclose(file_pointer); A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer. Observe the following program. …. FILE *p1 *p2; p1=fopen (“Input”,”w”); p2=fopen (“Output”,”r”); …. fclose(p1); fclose(p2) ;

  19. Reading & writinggetc and putc functions The getc and putc functions are analogous to getchar and putchar functions and handle one character at a time. The putc function writes the character contained in character variable c to the file associated with the pointer fp1. Example putc(c,fp1); The getc function is used to read a character from a file that has been open in read mode. c=getc(fp2).

  20. Example #include< stdio.h > main() { file *f1; printf(“Data input output”); f1=fopen(“Input”,”w”); /*Open the file Input*/ while((c=getchar())!=EOF) /*get a character from key board*/ putc(c,f1); /*write a character to input*/ fclose(f1); /*close the file input*/ printf(“nDataoutputn”); f1=fopen(“INPUT”,”r”); /*Reopen the file input*/ while((c=getc(f1))!=EOF) printf(“%c”,c); fclose(f1); } Check for feof and ferror functions End_of_File

  21. Reading & writing The getw and putw functions • These are integer-oriented functions. They are similar to get c and putc functions and are used to read and write integer values. These functions would be usefull when we deal with only integer data. The general forms of getw and putw are: • putw(integer,fp); getw(fp);

  22. Reading & writing The getw and putw functions /*Example program for using getw and putw functions*/ #include< stdio.h > main() { FILE *f1,*f2,*f3; intnumber I; printf(“Contents of the data filenn”); f1=fopen(“DATA”,”W”); for(I=1;I< 30;I++) { scanf(“%d”,&number); if(number==-1) break; putw(number,f1); } fclose(f1); f1=fopen(“DATA”,”r”); f2=fopen(“ODD”,”w”); f3=fopen(“EVEN”,”w”); while((number=getw(f1))!=EOF)/* Read from data file*/ { if(number%2==0) putw(number,f3);/*Write to even file*/ else putw(number,f2);/*write to odd file*/ } fclose(f1); fclose(f2); fclose(f3); f2=fopen(“ODD”,”r”); f3=fopen(“EVEN”,”r”); printf(“nnContents of the odd filenn”); while(number=getw(f2))!=EOF) printf(“%d%d”,number); printf(“nnContents of the even file”); while(number=getw(f3))!=EOF) printf(“%d”,number); fclose(f2); fclose(f3); }

  23. fprintf & fscanf functions • The fprintf and fscanf functions are identical to printf and scanf functions except that they work on files. • The first argument of theses functions is a file pointer which specifies the file to be used. • The general form of fprintf is fprintf(fp,”control string”, list); Where fp id a file pointer associated with a file that has been opened for writing. The control string is file output specifications list may include variable, constant and string. • fprintf(f1,%s%d%f”,name,age,7.5); Here name is an array variable of type char and age is an int variable

  24. fprintf & fscanf functions • The general format of fscanf is fscanf(fp,”controlstring”,list); This statement would cause the reading of items in the control string. Example: fscanf(f2,”5s%d”,item,&quantity”); • Like scanf, fscanf also returns the number of items that are successfully read.

  25. /*Program to handle mixed data types*/ #include< stdio.h > main() { FILE *fp; intnum,qty,I; float price,value; char item[10],filename[10]; printf(“Input filename”); scanf(“%s”,filename); fp=fopen(filename,”w”); printf(“Input inventory datann”0; printf(“Item namem number price quantityn”); for I=1;I< =3;I++) { scanf(”%s%d%f%d”,item,&number,&price,&quality); fprintf(fp,”%s%d%f%d”,itemnumber,price,quality); } fclose (fp); fp=fopen(filename,”r”); printf(“Item name number price quantity value”); for(I=1;I< =3;I++) { fscanf(fp,”%s%d%f%d”,item,&number,&prince,&quality); value=price*quantity”); printf(”%s%d%f%d%dn”,item,number,price,quantity,value); } fclose(fp); }

  26. Types of file There are two types of files:- • Text files-A text file is a stream of characters that can be sequentially processed by a computer in forward direction.Because text files only process characters they can only read or write data one character at a time. • Binary files:- Binary file may contain any type of data ,encoded in binary form for computer storage and processing purposes. Binary file is a collection of bytes.

  27. Some More file modes • rb- open a binary file for reading • wb-open a binary file for writing • ab-Append a binary file • r+ - open a text file for both reading and writing. The stream will be positioned at the beginning of the file. When you specify ‘r+’ , you indicate that you want to read the file before you write on it. • W+ -open a text file for both reading and writing.The stream will be created if it does not exist and will be truncated if it exists. • a+ - open a file for both reading and writing. The stream will be positioned at the end of the file content. • r+b/rb+ - open a binary file for read/write • w+b/wb+ - create a binary file for read/write • a+ b/ab+ - Append a binary file for read and write.

  28. Fgets() and fputs function The function fgets()stands for file get string. The fgets() function is used to get a string from a stream. The syntax is Char*fgets(char*str, int size, FILE *stream); fgets() terminates either by reading one less than the no. of characters specified by size or encounters a new line or EOF or any error. For eg. fgets(str,80,fp) Fputs() is used to write a line to a file. The syntax is Int fputs(const char *str,FILE *stream);

  29. Example #include<stdio.h> Void main() { FILE *fp; Char Answer[50]; Fp=fopen(“explain.txt”,”w”); If (fp==NULL) { printf(“\n the file could not be opened”); Exit(1); } Printf(“\n give answer”); gets(answer); fputs(answer,fp); fclose(); }

  30. Error handling during file operation • ferror()-it is used to check for errors in the stream. • Clearerr()- it is used to clear the end-of-file and error indicators for the stream.

  31. Example Void main() { FILE *fp; Char Answer[50]; Fp=fopen(“explain.txt”,”w”); If (fp==NULL) { printf(“\n the file could not be opened”); Exit(1); } Printf(“\n give answer”); gets(answer); For(i=0;i<answer[i];i++) fputc(answer[i],fp); If(ferror(fp)) {clearerr(fp); Break; } } fclose(); }

  32. Fread()and fwrite() • Fread() function is used to read data from the file. Its syntax can be given as Intfread(void *str,size_t,size_tnum,FILE *stream); Fread() reads num number of objects(where each object is of size bytes) and places them into the array pointed to by str. Fwrite() function is used to write data to a file. The syntax is Intfwrite(const void *str,size_tsize,size_tcount,FILE * stream) Fwrite () function will write objects ( no. of objects will be specified by count) of size specified by size from the stream

  33. A program to write and read details of student from the file Void main() { FILE * fp1; Typedef struct student { int roll_no; char name[80]; Int marks; } STUDENT; STUDENT S; Char another =‘y’; Fp1=fopen(“student”,wb); While(another ==‘y’) {printf(“enter the name ,age ,basic salary”); Scanf(“%s,%d,%f”,e.name,&e.age, &e.bs); Fwrite(&e,sizeof(e) ,1 ,fp); Printf(“want to add another record(y/n)”); Fflush(stdin); Another=getche(); } Fclose(fp); Fp1=fopen(“student”,rb); While(!feof(fp)) {Fread(&e,sizeof(e) ,1 ,fp); printf(“%s,%d,%f”,e.name,e.age,e.bs); } Fclose(fp); }

  34. Random access to files • Sometimes it is required to access only a particular part and not the complete file. This can be accomplished by using the following function: • fseekfunction: The general format of fseek function is a s follows: fseek(file pointer,offset, position);

  35. Random access to files This function is used to move the file position to a desired location within the file. Fileptr is a pointer to the file concerned. Offset is a number or variable of type long, and position in an integer number. Offset specifies the number of positions (bytes) to be moved from the location specified by the position. The position can take the 3 values. • Value Meaning 0 Beginning of the file 1 Current position 2 End of the file.

  36. Random access to files • Ftell()-this function tells the current position of file pointer. Syntax- long ftell(FILE *stream) If successful ftell() function returns the current file position (in bytes) for stream. However in case of error , ftell() returns -1.

  37. Random access to files • Rewind()- this function is used to set the file pointer to the beginning of the file Syntax- void rewind(FILE * F) It is equivalent to calling the fseek function as Fseek(f,0,0); • Remove()- as the name suggest it is used to erase a file. Syntax: remove(const char* file name)

  38. Random access to files Rename()- this function is used to rename a file. Int rename(const char *oldname,const char *newname); Tmpfile()- this function is used to create a temporary file with access parameter set as “w+”. The file created will be automatically closed and erased when the program has been completely executed.

  39. The Preprocessor The #include Preprocessor Directive The #define Preprocessor Directive: Symbolic Constants The #define Preprocessor Directive: Macros Conditional Compilation The #error and #pragma Preprocessor Directives The # and ## Operators Line Numbers Predefined Symbolic Constants Assertions

  40. Introduction • Preprocessing • Occurs before program compiled • Inclusion of external files • Definition of symbolic constants • Macros • Conditional compilation • Conditional execution • All directives begin with # • Can only have whitespace before directives • Directives not C statements • Do not end with ;

  41. The #include Preprocessor Directive • #include directive • Puts copy of file in place of directive • Seen many times in example code • Two forms • #include <filename> • For standard library header files • Searches predesignated directories • #include "filename" • Searches in current directory • Normally used for programmer-defined files

  42. The #include Preprocessor Directive • Usage • Loading header files • #include <iostream> • Programs with multiple source files • Header file • Has common declarations and definitions • Classes, structures, enumerations, function prototypes • Extract commonality of multiple program files

  43. The #define Preprocessor Directive: Symbolic Constants • #define • Symbolic constants • Constants represented as symbols • When program compiled, all occurrences replaced • Format • #define identifier replacement-text • #define PI 3.14159 • Everything to right of identifier replaces text • #define PI=3.14159 • Replaces PI with "=3.14159" • Probably an error • Cannot redefine symbolic constants

  44. The #define Preprocessor Directive: Symbolic Constants • Advantages • Takes no memory • Disadvantages • Name not be seen by debugger (only replacement text) • Do not have specific data type • const variables preferred

  45. The #define Preprocessor Directive: Macros • Macro • Operation specified in #define • Intended for legacy C programs • Macro without arguments • Treated like a symbolic constant • Macro with arguments • Arguments substituted for replacement text • Macro expanded • Performs a text substitution • No data type checking

  46. The #define Preprocessor Directive: Macros • Example #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) area = CIRCLE_AREA( 4 ); becomes area = ( 3.14159 * ( 4 ) * ( 4 ) ); • Use parentheses • Without them, #define CIRCLE_AREA( x ) PI * x * x area = CIRCLE_AREA( c + 2 ); becomes area = 3.14159 * c + 2 * c + 2; which evaluates incorrectly

  47. The #define Preprocessor Directive: Macros • Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); becomes rectArea = ( ( a + 4 ) * ( b + 7 ) ); • #undef • Undefines symbolic constant or macro • Can later be redefined

  48. Conditional Compilation • Control preprocessor directives and compilation • Cannot evaluate cast expressions, sizeof, enumeration constants • Structure similar to if #if (!defined( NULL )) #define NULL 0 #endif • Determines if symbolic constant NULL defined • If NULL defined, • defined( NULL ) evaluates to 1 • #define statement skipped • Otherwise • #define statement used • Every #if ends with #endif

  49. Conditional Compilation • Can use else • #else • #elif is "else if" • Abbreviations • #ifdef short for • #if defined(name) • #ifndef short for • #if !defined(name)

  50. The #error Preprocessor Directives • #error string The error messages include the argument string. #error directive is usually used to detect programming inconsistencies and violation of constraints during preprocessing. Eg. #ifndef SQUARE #error Macro not defined #endif

More Related