170 likes | 297 Views
B Smith: 11/5/04. Lecture a 3. Fun for some, boring for others. Good lecture for introducing the idea of streams, so that in C++ ostream objects and istream objects are more intuitive. B Smith:
E N D
B Smith: 11/5/04. Lecture a 3. Fun for some, boring for others. Good lecture for introducing the idea of streams, so that in C++ ostream objects and istream objects are more intuitive. B Smith: 4/15/05: Rate: 3. The extra slides at end were not originally part of this. Incorporate these and discuss the keyboard input buffer and the residual \n B Smith: 11/5: Finish up the last slide which discussed why EOF gets printed. File Processing - II Math 130
Overview • File Processing • Random File Access • fseek() • ftell() • rewind() • Files as streams
RECALL: Reading data from a file #include <stdlib.h> #include <stdio.h> int main() { char descrip[10]; float price; FILE *inFile; inFile = fopen("prices.txt ","r"); if (inFile == NULL) { printf("\nFailed to open the file.\n"); exit(1); } while (fscanf(inFile,"%s %f",descrip,&price) != EOF) printf("%-9s %5.2f\n",descrip,price); fclose(inFile); return 0; } Why/How does this work? (zoom in)
fscanf() • When does the while loop terminate? • How does fscanf process the input? char descrip[10]; float price; FILE *inFile; inFile = fopen("prices.txt ","r"); while (fscanf(inFile,"%s %f", descrip, &price) != EOF) printf("%-9s %5.2f\n",descrip, price);
The Data File • Last class a very simple data file was created:
Random File Access • File Organization • Files may not always be organized per the previous example (i.e., sequentially) • File Access • Although the characters were sequential, the C library provides routines that allow the programmer to randomly access file data • The related functions: • rewind(),fseek(), and ftell()
File Access • Look at a file as a “stream” of data • FILE* keeps a record of the status of the file stream • start of file, current position, name of file, status 42 61 74 74 65 72 69 65-73 20 33 39 2E 39 35 0D Batteries 39.95. 0A 42 75 6C 62 73 20 20-20 20 20 20 33 2E 32 32 .Bulbs 3.22 0D 0A 46 75 73 65 73 20-20 20 20 20 20 31 2E 30 ..Fuses 1.0 33 0D 0A 00 00 00 00 00-00 00 00 00 00 00 00 00 3...............
rewind() • Resets the “current position” to the start of file • Uses the FILE* (the stream) as an argument: • rewind( inFile ); • The next character accessed will be the first character in the file (the stream)
fseek() • Allows you to move to any position in the file (the stream) • Each character has a position (think of them as being stored in a array) • Each character postion is its offset from the start of the file
fseek() • Allows you to move to any position in the file • Requires three arguments: • name of the file pointer • an offset • an “origin”; can think of as an absolute position to which an offset will be added
fseek(fileName, offset, origin) fseek(inFile, 4L, SEEK_SET); fseek(inFile, 4L, SEEK_CUR); fseek(inFile, -4L, SEEK_CUR); fseek(inFile, 0L, SEEK_SET); fseek(inFile, 0L, SEEK_END); fseek(inFile, -10L, SEEK_END); Options for origin: SEEK_SET – seek relative to start of file SEEK_CUR – seek relative to current position SEEK_END – seek relative to end of file Options for offset: The prototype specifies a long integer, and the value can be positive or negative
fseek() example int main(int argc, char* argv[]) { int i,j; FILE * pFile; pFile = fopen ("myfile.txt","w"); fputs ("This is an apple.",pFile); fseek (pFile,9,SEEK_SET); fputs (" sam",pFile); fclose (pFile); system("pause"); return EXIT_SUCCESS; } // After this code is executed, a file called example.txt // will be created and will contain the sentence. // “This is a sample.” (ref. cplusplus.com)
ftell() • Returns the offset of the next character that will be read or written • If command ftell(inFile) returns the integer 10 • the next character to be read is offset 10 bytes from the start of the file (the 11th character pos’n) • Given i = ftell(inFilel); and i = 4, what character will be read next?
B Smith: Where is the file ptr positioned? Why is EOF printed out and not CR/LF? int main() { int ch, n; long int offset, last; FILE *inFile; inFile = fopen("test.dat","r"); fseek(inFile,0L,SEEK_END); /* move to the end */ last = ftell(inFile); /* last = size of file */ for(offset = 0; offset <= last; ++offset) { fseek(inFile, -offset, SEEK_END); /* move to next character */ ch = getc(inFile); /* get the character */ switch(ch) { case '\n': printf("LF : "); break; case EOF : printf("EOF: "); break; default : printf("%c : ",ch); break; } } fclose(inFile); return 0; } pgm 11.5
Examples • findSize.c • fp2.c • fileReading2.c • fseek.c • from cplusplus.com
Summary • File Processing • Random File Access • fseek() • ftell() • rewind() • Files as streams • NEXT: • C++ and Object Oriented Programming