cs1061 c programming lecture 18: sequential file processing

13
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated

Upload: zena-odonnell

Post on 30-Dec-2015

37 views

Category:

Documents


2 download

DESCRIPTION

CS1061 C Programming Lecture 18: Sequential File Processing. A. O’Riordan, 2004, 2007 updated. The FILE structure. C views each file as a sequence of bytes Files end with the end-of-file marker (EOF) Stream created when a file is opened Opening a file returns a pointer to a FILE structure - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1061 C Programming Lecture 18: Sequential File Processing

CS1061 C ProgrammingLecture 18: Sequential File Processing

A. O’Riordan, 2004, 2007 updated

Page 2: CS1061 C Programming Lecture 18: Sequential File Processing

The FILE structure

C views each file as a sequence of bytes Files end with the end-of-file marker (EOF) Stream created when a file is opened Opening a file returns a pointer to a FILE structure

The FILE structure File descriptor

index into operating system array called the open file table File Control Block (FCB)

found in every array element, system uses it to administer the file

Page 3: CS1061 C Programming Lecture 18: Sequential File Processing

Creating a File

FILE *myPtr;creates a FILE pointer called myPtr

myPtr = fopen("myFile.dat", openmode);

Function fopen() returns a FILE pointer and takes two arguments – file to open and file open mode.

If open fails, NULL returned.

fclose(FILE pointer)Function fclose() closes specified file.Even though it’s performed automatically when program ends it is good practice

to close files explicitly.

Page 4: CS1061 C Programming Lecture 18: Sequential File Processing

Reading a sequential access file

A sequential access file is written and read from the start to the end of the file using a file pointer.

Create a FILE pointer, tie it to the file to read

myPtr = fopen("myFile.dat", "r");

Use fscanf() to read from the file.

Like scanf(), except first argument is a FILE pointer, e.g.:

fscanf(myPtr, "%d%s%f", &myInt, myString, &myFloat);

Page 5: CS1061 C Programming Lecture 18: Sequential File Processing

fopen()

Every call to fopen() will typically be followed with a test, like this:

ifp = fopen("input.dat", "r");if(ifp == NULL){

printf("can't open file\n");//exit or return}

or

if((ifp = fopen("input.dat", "r")) == NULL){printf("can't open file\n");//exit or return

}

Page 6: CS1061 C Programming Lecture 18: Sequential File Processing

File Modes

Files are open in a certain mode.

MODE USED FOR FILE CREATED? EXISTING FILE? "a" Appending Yes

Appended to "a+" Reading/appending Yes

Appended to "r" Read only No Yes"r+" Reading/writing No Yes"w" Write only Yes Destroyed "w+" Reading/writing Yes Destroyed

The number of streams that may be open simultaneously is specified by the macro FOPEN_MAX (in stdio.h). FOPEN_MAX must be at least eight in ISO C.

Page 7: CS1061 C Programming Lecture 18: Sequential File Processing

fscanf() and fprintf()

fscanf() and fprintf() are file processing equivalents of scanf() and printf().

Prototypes (as in stdio.h):

int fprintf(FILE *stream, const char *format, ...);

int fscanf(FILE *stream, const char *format, ...);

fprintf(stdout, "Hello") is eqivalent to printf(“Hello")

fscanf(stdin, “%d”, &i) is the equivalent of scanf( “%d”, &i)

The following example outputs a table of exam scores to a file named scores.txt.

Page 8: CS1061 C Programming Lecture 18: Sequential File Processing

fprintf() example

#include <stdio.h>

#include <stdlib.h>

int main(){

int i, score[10];

FILE *HiScores;

for(i=0; i<10; i++) score[i] = 100 - i;

if(!(HiScores = fopen("scores.txt", "w"))== NULL){

for(i=0; i<10; i++)

fprintf(HiScores, "Number %d:\t %d\n", i+1, score[i]);

return 0;

}

Page 9: CS1061 C Programming Lecture 18: Sequential File Processing

fscanf() example

#include <stdio.h>

int main(){int i;FILE *fp;

fp = fopen("scores.txt", "r");

while(fscanf(fp, "%d", &i) != EOF)printf("%d\n", i);

return 0;}

Page 10: CS1061 C Programming Lecture 18: Sequential File Processing

fgetc() and fputc()

fgetc() reads one character from a file. Takes a FILE pointer as an argument.

fgetc() and getc() are identical, except that getc() is usually implemented as a macro for efficiency.

fputc() writes one character to a file Takes a FILE pointer and a character to write as an argument. Likewise fputc() and putc() are identical.

Page 11: CS1061 C Programming Lecture 18: Sequential File Processing

fgetline()

Read one line from data.txt, copying it to line array (not more than max chars). Does not place terminating \n in line array. Returns line length, or 0 for empty line, or EOF for end-of-file.

Now we could read one line from ifp by calling:#define MAXLINE 100char line[MAXLINE];ifp = fopen(“data.txt”, “r”);...fgetline(ifp, line, MAXLINE);

EOF indicates the end of file. It usually has the value -1. (ISO C standard requires only that it's a negative integer.)

Page 12: CS1061 C Programming Lecture 18: Sequential File Processing

fgetc() example

int fgetline(FILE *fp, char line[], int max){int nch = 0, c;max = max - 1; /* leave room for '\0' */

while((c = fgetc(fp)) != EOF){if(c == '\n')

break;if(nch < max){

line[nch] = c;nch = nch + 1;}

}if((c == EOF) && (nch == 0))

return EOF;line[nch] = '\0';return nch;}

Page 13: CS1061 C Programming Lecture 18: Sequential File Processing

Caution

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.

Because of these limitations C also has random access file processing features. See next lecture.