1 / 61

File Processing

File Processing. Introduction. Data files Can be created, updated, and processed by C programs Are used for permanent storage of large amounts of data (floppy disk, hard disk, …). Storage of data in variables and arrays and structures is only temporary. Files.

Télécharger la présentation

File Processing

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. File Processing

  2. Introduction • Data files • Can be created, updated, and processed by C programs • Are used for permanent storage of large amounts of data (floppy disk, hard disk, …). • Storage of data in variables and arrays and structures is only temporary

  3. Files • C views each file as a sequence of bytes • File ends with the end-of-file marker

  4. There are two types of files: text files (ascii files and sequential files) • Data of various types is usually stored as a sequence of characters • the file is treated as a sequence of characters or as a sequence of values. • Data access is usually processed sequentially. binary files (random access files) • the file is treated as a sequence of bytes representing the data. • char values are stored in 1 byte, float values in 4 bytes, … • Data is usually accessed randomly: a location can be accessed directly without accessing preceding bytes in the file.

  5. The Data Hierarchy • Data Hierarchy: • Bit – smallest data item • Value of 0 or 1 • Byte – 8 bits • Used to store a character • Decimal digits, letters, and special symbols • Field – group of characters conveying meaning • Example: your name • Record – group of related fields • Represented by a struct or a class • Example: In a payroll system, a record for a particular employee that contained his/her identification number, name, address, etc.

  6. The Data Hierarchy • Data Hierarchy (continued): • File – group of related records • Example: payroll file • Database – group of related files

  7. The Data Hierarchy • Data files • Record key • Identifies a record to facilitate the retrieval of specific records from a file • Sequential file (text file) • Records typically sorted by key

  8. Creating a Sequential Access File • Programs may process no files, one file, or many files • Each file must have a unique name and should have its own pointer • Opening a file returns a pointer to a FILE structure

  9. Before a File can be Opened... • Like anything else in C, before you can manipulate a file you must declare it • Files are declared by declaring a File Handle, which is a pointer to FILE FILE *fp; • This creates a pointer in memory which will contain the address of a memory location through which the file can be communicated with • We can consider this area to be a file buffer, leaving to the Operating System the particulars of how the file is physically read from and written to • As usual, fp will initially contain garbage

  10. We create a File Handle by fopening a file fp = fopen (const char *filename, const char *mode); • filename is a character string representing the file name. • The full pathname of the file must be given if the file is not in the same directory as the executable

  11. mode is one of the following character strings

  12. Iffopen() is successful, thenfpwill contain the address through which the contents of the file can be manipulated (it will be a stream( • Streams • Sequences of characters organized into lines • Each line consists of zero or more characters and ends with newline character • Iffopen() fails thenfpwill beNULL • fopen() can fail if the file is opened for reading and the file does not exist

  13. Closing Files • Files can be closed using fclose(FILE *stream;( • fclose() will return 0 if the stream was closed successfully, otherwise it will returnEOF

  14. Notes • There are a maximum number of files that can be open at any one time, so closing files when you are done with them is very important • We'll also see thatstdin, stdoutandstderrare special files which C opens automatically

  15. Creating a File (summary) • FILE *fPtr; • Creates a FILE pointer called fPtr • fPtr = fopen("c:\\students.dat","w"); • Function fopen returns a FILE pointer to file specified • Takes two arguments – file to open and file open mode • If open fails, NULL is returned • fclose(fptr) • Closes specified file • Performed automatically when program ends • Good practice to close files explicitly • Takes as argument a pointer to file specified

  16. Read/Write functions in standard library (stdio.h) • fscanf / fprintf • fgetc • int fgetc ( FILE * stream ); • fputc • int fputc ( int character, FILE * stream ); • fgets • char *fgets(char *s, int n, FILE *stream); • The function terminates reading either after a new-line character is found or end-of-file is reached, or after (length - 1) characters have been read. • fputs • int fputs ( const char * str, FILE * stream );

  17. fscanf / fprintf • File processing equivalents of scanf and print • fprintf • Used to print to a file • Like printf, except first argument is a FILE pointer (pointer to the file you want to print in) • fscanf • Used to read from the file • Like scanf, except first argument is a FILE pointer (pointer to the file you want to read from)

  18. Example 1: Creating a simple text file. #include <stdio.h> void main(){ FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”,”w”); if (fptr==NULL) printf( "File could not be opened\n" ); else{ fprintf(fptr, “%s %d\n”, “ali”, 70); fprintf(fptr, “%s %d”, “ahmad”, 80); fclose(fptr); }//else }//main

  19. Example 1: reading a text file (from File to Screen) #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int score; FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”, ”r”); if (fptr==NULL) printf( "File could not be opened\n" ); else{ fscanf(fptr, “%s %d”, name, &score); printf( "%s %d", name, score ); //cout<<name<<" "<<score<<endl; fscanf(fptr, “%s %d”, name, &score); printf( "%s %d", name, score ); //cout<<name<<" "<<score<<endl; fclose(fptr); }//else }//main

  20. Example 2: reading data from KB into a text file. #include <stdio.h> #include <iostream.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”, ”w”); if (fptr==NULL) printf( "File could not be opened\n" ); else{ for(i=1;i<=100;i++){ cin>>name>>score; fprintf( fptr, “%s %d\n”, name, score); }//for fclose(fptr); }//else }//main

  21. Example 2: printing data from text file into screen #include <stdio.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”, ”r”); if (fptr==NULL) printf( "File could not be opened\n" ); else{ for(i=1;i<=100;i++){ fscanf(fptr, “%s %d”, name, &score); cout<<name<<" "<<score<<endl; }//for fclose(fptr); }//else }//main

  22. feof( FILE pointer ) • Returns true if end-of-file indicator is set for the specified file (no more data to process) feof(fptr) • Returns true if end-of-file indicator is set for the file to which fptr points

  23. Example 2: printing data from text file into screen (2) #include <stdio.h> void main(){ char name[20]; int i; int score; FILE * fptr=NULL; fptr=fopen(“c:\\students.dat”, ”r”); if (fptr==NULL) printf( "File could not be opened\n" ); else{ fscanf(fptr, “%s %d”, name, &score); while(!feof(fptr)){ cout<<name<<" "<<score<<endl; fscanf( fptr, “%s %d”, name, &score); }//while fclose(fptr); }//else }//main

  24. Enter the account, name, and balance. Enter EOF to end input. ? 100 Jones 24.98 ? 200 Doe 345.67 ? 300 White 0.00 ? 400 Stone -42.16 ? 500 Rich 224.62 ? ^Z

  25. End of File (EOF) Key Combinations • Unix: <return><ctrl>d • Dos: <ctrl>z • Macintosh: <ctrl>d • Windows: <ctrl>z

  26. Account Name Balance 100 Jones 24.98 200 Doe 345.67 300 White 0.00 400 Stone -42.16 500 Rich 224.62

  27. rewind Statement • It may be desirable to process the data sequentially in a file several times (from the beginning of the file) during the execution of a program. • File position pointer • Indicates number of next byte to be read / written • It is not really a pointer, but an integer value (specifies byte location) • Also called file offset • rewind (FILE pointer); - Repositions/resets file position pointer to beginning of file (byte 0)

  28. Enter request 1 - List accounts with zero balances 2 - List accounts with credit balances 3 - List accounts with debit balances 4 - End of run ? 1 Accounts with zero balances: 300 White 0.00 ? 2 Accounts with credit balances: 400 Stone -42.16 ? 3 Accounts with debit balances: 100 Jones 24.98 200 Doe 345.67 500 Rich 224.62 ? 4 End of run.

  29. Example (a) Append, open or create a file for writing at the end of the file #include <stdio.h> int main() { int account; /* account number */ char name[ 30 ]; /* account name */ double balance; /* account balance */ FILE *cfPtr; /* cfPtr = clients.dat file pointer */ if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) { printf( "File could not be opened\n" ); } else { printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance );

  30. //write account, name and balance into file with fprintf while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); } fclose( cfPtr ); }

  31. if ( ( cfPtr = fopen( "clients.dat", "a" ) ) == NULL ) { printf( "File could not be opened\n" ); } else{ printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); //write account, name and balance into file with fprintf while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); } fclose( cfPtr ); } }

  32. Example (w+) Create a file for update, if the file already exists, discard the current contents #include <stdio.h> void main() { int account; /* account number */ char name[ 30 ]; /* account name */ double balance; /* account balance */ FILE *cfPtr; /* cfPtr = clients.dat file pointer */ if ( ( cfPtr = fopen( "clients.dat", "w+" ) ) == NULL ) { printf( "File could not be opened\n" ); } else { printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance );

  33. //write account, name and balance into file with fprintf while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); } rewind( cfPtr ); cout<<"\nthe file contents are\n"; fscanf(cfPtr,"%d%s%lf", &account, name, &balance ); while(!feof(cfPtr)){ cout<<account<<" "<<name<<" "<<balance<<endl; fscanf(cfPtr,"%d%s%lf", &account, name, &balance ); }//while fclose(cfPtr); } }

  34. #include <stdio.h> #include <iostream.h> #include<string.h> void main() { int account; // account number char name[ 30 ]; // account name double balance; // account balance FILE *cfPtr; // cfPtr = clients.dat file pointer if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) { printf( "File could not be opened\n" ); } else { printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); Example: Write a program to find the customer with the maximum account in the file.

  35. //write account, name and balance into file with fprintf while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n", account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance ); } fclose(cfPtr); }

  36. if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) { printf( "File could not be opened\n" ); } else{ int max=0; char maxSt[80]; while(!feof(cfPtr)){ fscanf(cfPtr,"%d%s%lf", &account, name, &balance ); if(max<account){ max=account; strcpy (maxSt, name); }//if }//while cout<<maxSt<<" is the customer with the max account “<<max<<endl; fclose( cfPtr ); }//else }//main

  37. 300 White 0.00 400 Jones 32.87 (old data in file) If we want to change White's name to Worthington, 300 White 0.00 400 Jones 32.87 Data gets overwritten 300 Worthington 0.00ones 32.87 300 Worthington 0.00 Note • Sequential access file • Cannot be modified without the risk of destroying other data • Fields can vary in size • Different representation in files and screen than internal representation • 1, 34, -890 are all ints, but have different sizes on disk

  38. Read/Write functions in standard library (stdio.h) • fscanf / fprintf • fgetc / fputc • Fgets / fputs

  39. Examples: Reading from a stream int fgetc(FILE *stream); • fgetc() reads a character from the stream, converting it to anintin the process (so that it is the ASCII value of the character( • fgetc() returnsEOFwhen the end-of-file is encountered • char c; FILE *fp; fp = fopen("filename", "r"); c = fgetc(fp); while (c != EOF){ c = fgetc(fp); } • fgetc() returnEOFwhenever an error occurs...

  40. This program uses getc to read the first 80 input characters (or until the end of input) and place them into a string named buffer. #include <stdio.h> #include <stdlib.h> void main( void ) { FILE *stream; char buffer[81]; int i, ch; /* Open file to read line from: */ if( (stream = fopen( "fgetc.txt", "r" )) == NULL ) exit( 0 ); /* Read in first 80 characters and place them in "buffer": */ ch = fgetc( stream ); for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ ) { buffer[i] = (char) ch; ch = fgetc( stream ); } /* Add null to end string */ buffer[i] = '\0'; printf( "%s\n", buffer ); fclose( stream);}

  41. Writing to a stream int fputc(int c, FILE *stream); • fputc() writes c to the stream • fputc() returns the character just written if the write was successful, and EOF otherwise

  42. Example: This program uses fputc to send a character array to stdout. #include <stdio.h> void main( void ) { char strptr1[] = "This is a test of fputc!!\n"; char *p; /* Print line to stream using fputc. */ for( p = strptr1;(*p != '\0'); p++) fputc( *(p), stdout ) ; }

  43. Reading from Standard Input • Whenever a C program executes, three files are automatically opened for the process stdin: Standard inputstdout: standard outputstderr: Standard error

  44. They are already streams, so you can read from stdin... fgetc(stdin) ... write to stdout... fputc(‘c’, stdout) • stdin, stdout, and stderr are defined in stdio.h

More Related