Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()

Similar presentations


Presentation on theme: "Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()"— Presentation transcript:

1 Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc() and fputc() Using fread() and fwrite()

2 Files and Streams C views a file as a sequential stream of bytes. A file ends either with an EOF (end-of-file) marker or at a specified byte number specified by the system. When a file is opened, a stream is associated with a file. Streams provide communication channels between files and the programs.

3 In addition to providing access to a file, a stream can also be used to access devices. For example, when a program (any program) is executed, 3 streams are automatically opened: –standard input (stdin) enable the program to read data from keyboard –standard output (stdout) enable the program to print data on the screen –standard error (stderr) enable program to print errors on the screen They are all manipulated using file pointers.

4 Creating a Sequential Access File #include void main (void) { int account; char name[30]; float balance; FILE *f; if ((f = fopen(“clients.txt”, “w”)) == NULL) printf(“Error: the file cannot be opened\n”); else { printf (“Enter the account, name and balance or EOF to end input:\n”); scanf (“%d%s%f”, &account, name, &balance); while (!feof(stdin)) { fprintf(f, “%d %s %.2f”, account, name, balance); scanf (“%d%s%f”, &account, name, &balance); } fclose (f); }

5 FILE *f; –States that f is a pointer to a file structure –If there is more than one file, each file needs to have its own FILE pointer. if ((f = fopen(“clients.txt”, “w”)) == NULL) –The fopen() function takes 2 arguments: the file name and the file mode. –This statement will try to open a file named “clients.txt”. –If the file is to be placed in a different directory than the program directory, the full path need to be specified. For example: “A:\clients.txt” –The function returns a pointer to the successfully opened file. But if the file cannot be opened, a NULL is returned. –The option “w” is the mode of the file to be opened.

6 while (!feof(stdin)) –Determines whether the input from the keyboard is an EOF character or not. If it is an EOF character, the while loop will terminate. –An EOF character is not the word “EOF” !!!

7 fprintf (f, “%d %s %.2f\n”, account, name, balance); –Writes data to the file clients.dat –Equivalent to printf() except that it receives as an argument a file pointer for the file to which the data will be written. fclose(f); –Closes the file “clients.txt”. –Will actually write the data to the file. –If fclose() is not called explicitly, the operating system will close the file when the program terminates.

8 If an error occurs while opening a file (in which case fopen() returns a NULL), it could be due to any of these errors: –Opening a non-existing file for reading –Opening a file for reading or writing without having granted the appropriate access to the file by the operating system. –Opening a file for writing when no disk space is available. Always remember that the mode “w” will overwrite the current data in the file. When we want to update a file, always use the update mode “r+”.

9 Reading Data from a Sequential Access File void main (void) { int account; char name[30]; float balance; FILE *fptr; if ((fptr = fopen(“clients.txt”, “r”)) == NULL) printf(“Error: the file cannot be opened\n”); else { printf (“%-10s%-13s%s\n”, “Account”, “Name”, “Balance”); fscanf (fptr, “%d%s%f”, &account, name, &balance); while (!feof(fptr)) { printf (“%-10s%-13s%s\n”, “Account”, “Name”, “Balance”); fscanf (fptr, “%d%s%f”, &account, name, &balance); } fclose (fptr); }

10 if ((fptr = fopen(“clients.txt”, “r”)) == NULL) ; –Attempts to open the file clients.txt for reading and determines whether the file is opened successfully. Notice that the mode used is “r”. fscanf (fptr, “%d%s%f”, &account, name, &balance); –Reads data from the file clients.txt –Equivalent to function scanf() except that it receives as an argument a file pointer for the file from which the data is read.

11 Using fgetc() and fputc() Syntax: –int fgetc(FILE *f) –fputc(int c, FILE *f) fgetc() reads a character from the file referred to by the file pointer f. fputc() puts the character c into the file referred to by the file pointer f. If the file has reached the end, fgetc() will return the EOF character.

12 Using fread() and fwrite() Syntax: –size_t fread (void *d, size_t s, size_t n, FILE *f) –size_t fwrite (const void *d, size_t s, size_t n, FILE *f) fread() reads n items of size s into the memory pointed to by d. The function returns the total size of data that has been read. fwrite writes n items of size s from the memory pointed to by d. The function returns the total size of data that has been successfully written.


Download ppt "Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()"

Similar presentations


Ads by Google