Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files Chapter 8.

Similar presentations


Presentation on theme: "Files Chapter 8."— Presentation transcript:

1 Files Chapter 8

2 Files Information can be stored on disk in the form of a data FILE
Data files can be read by programs, written to by programs and updated by programs A file is similar to a table with rows and columns A row is referred to as a record A column is referred to as a field

3 Use of Files in C Program
Declare file (once, at beginning of program) Open file (once or more, normally at beginning of program) Read from or write to file (or both) Close file (once or more, normally at end of pgm)

4 Step 1: Declare file(s) Need to declare a pointer which will hold the address of the file. Example: FILE *fp_name, *fp_rept;  the pointers fp_name and fp_rept will each hold the starting disk address of a file

5 Step 2: Open file using fopen
fopen command establishes a connection with a file and assigns the address of the file to a pointer. 1st parameter: name of physical file used by operating system 2nd parameter: access mode where: r: read the file (if file not found by OS, then open statement will fail) w: write to the file (if file already exists its contents will be overwritten) a: append, i.e. add at end of existing file (if file not found by OS, file will be created)

6 Step 2 (ctd): fopen Example: fp_name = fopen(“names.dat”, “r”);
  fp_rept = fopen(“report.dat”, “w”); Opens OS file names.dat in read mode and puts the address of the file in fp_name. Opens OS file report.dat in write mode and puts the address of the file in fp_rept. NOTE: A file must be opened before it can be used!

7 Step 2 (ctd): fopen Value returned by fopen should always be checked because if a file cannot be opened, ‘NULL’ is returned by fopen function Example: fp_name = fopen(“names.dat”, “r”); if (fp_name == NULL) printf(“names.dat file not opened!”);

8 Step 3: Read data from file
fscanf is used to read from a file. Format is almost exactly the same as the scanf statement except must provide file pointer as 1st parameter. Example: fscanf(fp_name,“%s;%s;%d\n”,lname, fname, &studno);  This will read 2 strings separated by a semi-colon and assign them to the arrays fname and lname, will then skip over a semicolon and then assign an integer value to studno

9 Step 3: Write data to file
fprintf writes to a file. Example: fprintf(fp_rept, “%s %s:%d\n”, fname, lname, studno); Writes a record to the file report.dat containing 2 strings separated by a space, followed by a colon and an integer value for studno and a newline (each record in a file must be ended with a newline character)

10 Step 4: Close file using fclose
Close file to ensure that all data sent to the file is written and saved and that file is left in stable state by OS. Example: fclose(fp_name); fclose(pf_rept);

11 End-of-file Condition
fscanf returns an integer which indicates the number of variables returned. When there is no more data, fscanf returns -1. This is equal to the standard constant EOF. You should check for this when reading through a file. Normally want to read file until fscanf returns –1 and then close file.

12 Sample file program #include <stdio.h> #include <stdlib.h>
void main () { char first[31], last[31]; /* first and last name */ int studno, i; FILE *fp_name, *fp_rept; fp_name = fopen("names.dat","r"); fp_rept = fopen("report.dat","w"); if ( (fp_name == NULL) || (fp_rept == NULL)) { printf ("Cannot open file\n"); exit(1); } while (fscanf(fp_name,"%s %s %d\n", last, first, &studno)!=-1) fprintf (fp_rept, "%d: %s %s\n", studno, first, last); fclose (fp_name); fclose (fp_rept); }


Download ppt "Files Chapter 8."

Similar presentations


Ads by Google