8 - ICS103 Programming in C Lecture 8 Data Files 1 Outline...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 8: Data Files
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Outline Why data files? Declaring FILE pointer variables Opening data files for input/output Scanning from and printing to data files Closing input and output files Echo Prints vs. Prompts Handling File not found error EOF-controlled Loops
Background image of page 2
3 Why data files? So far, all our examples obtained their input from the keyboard and displayed their output on the Screen. However, in many real-life applications, the input data is so much that it will be inconvenient to expect the user to type it each time the program is run. For example: A program to generate employee pay slip from employee data. Similarly, there are many applications where the output will be more useful if it is stored in a file rather than the screen. For example: In the program that generates pay slip, how can we print the pay slips and distribute them to the employees if the output is printed on the screen? The good news is that C allows a programmer to direct his program to use data files, both for input and for output.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Steps For Using Data Files The process of using data files for input/output involves four steps as follows: 1. Declare variables of type FILE to represent the files 2. Open the files for reading/writing using the fopen function. 3. Read/write from/to the files using the fscanf and fprintf functions. 4. Close the files after processing the data using the fclose function. In what follows, we explain each of these steps.
Background image of page 4
5 Declaring FILE pointer variables The first step in using data files for input/output is to declare variables to represent the files. This is done as follows: FILE *infile, //pointer variable for the input file *outfile; //pointer variable for the output file Note that the type for declaring file variables is FILE in upper case. Also note the use of ‘*’ just before the variable identifiers. This is used to indicate that the variables are pointer variables – they store memory addresses.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Opening data files for input/output The second step is to open the input file for reading and the output file for writing.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

8 - ICS103 Programming in C Lecture 8 Data Files 1 Outline...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online