Title: Lecture 11 File IO
1Lecture 11File I/O
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
10/15/01
2Outline
- Open a sequential access data file
- Write information to a sequential access data
file - Close a sequential access data file
- Read information from a sequential access data
file - Test for the end of an input file
3Fields, Records, and Data Files
- Field
- A single item of information about a person,
place, or thing - Example Name
- Record
- One or more related fields that contain all of
the necessary data about a specific person,
place, or thing - Example Name, USD ID, Grade
- Data file
- A collection of related records
- Three types sequential, random, binary
- Example ENGR17 Grade File (contains mostly Fs)
4Fields, Records, and Data Files
5Sequential Access Data Files
- Each record in the file is both stored and
retrieved in consecutive order (sequentially) - Advantage
- Easy to create
- Drawback
- Records can be processed only in the order in
which they are stored - Work best for processing either a small file
(lt100 records) or a large file whose records are
always processed in consecutive order
6Sequential Access Data Files
7Components of File Access
- Steps to perform when reading from or writing to
a sequential access file - Create File Objects
- Open File in appropriate mode
- Read
- Write
- Append
- Check if file open failed
- If it failed, end the function
- Read To or Write From File
- Close File
8Opening a Sequential Access Data File
- Must use include ltfstream.hgt
- Use the ifstream and ofstream classes to create
input or output file objects, respectively - Input data files Contents are read by a program
- Output data files files to which a program
writes data - After creating file objects, use the C open
function to open the file for either either
input, output, or append
9Creating File Objects
10open Function Modes
11Opening Files with the open Function
12Record Indicator
- Used to keep track of the next record either to
read or write in a data file
13Test If File Open Was Successful
- Use the C fail function, defined in both the
ifstream and ofstream classes - fail function returns
- Value 1 (for True) if the open failed
- Value 0 (for False) if the open did not fail
- Usually done immediately after opening the file
14Test If File Open Was Successful
15Write to a Sequential Access Data File
- Syntax for number or string
- object ltlt data ltlt endl
- Each record is typically written on a separate
line in the file to distinguish one from another - Just like printing output to the console using
cout
16Read from a Sequential Access Data File
- Syntax for reading a single number
- object gtgt data
- Syntax for reading an entire line (string)
- object.getline(data,sizeof(data))
- Using getline() reads the entire contents of the
line up to the newline character - Just like reading from cin
- Use eof to stop reading at end of file
17The eof Function
- Used only when reading a file
- Used to determine if the record indicator is at
the end of the input file - If a program attempts to read past the end of the
file, the eof function returns 1 (for True)
otherwise, it returns 0 (for False) - Used when the number of records in the file is
unknown
18Closing a File
- close function is defined in both the ifstream
and ofstream classes - Syntax
- object.close ()
- Not closing an open data file can result in loss
of data
19Example 1
- Write the numbers 0 to n-1 to a data file.
- include ltiostream.hgt
- include ltfstream.hgt
- void Write_Num_File(int n)
- void main ()
- Write_Num_File(5)
20Example 1
- void Write_Num_File(int n)
- int i
- char FileName20 "Data.txt"
- ofstream DataFile
- DataFile.open(FileName)
- if (DataFile.fail())
- cout ltlt "Unable to write to " ltlt FileName ltlt
endl - return
-
- for (i0 iltn i)
- DataFile ltlt i ltlt endl
- DataFile.close()
21Example 2
- Read numbers from a data file.
- include ltiostream.hgt
- include ltfstream.hgt
- void Read_Num_File()
- void main ()
- void Read_Num_File()
22Example 2
- void Read_Num_File()
- int value
- char FileName20 "Data.txt"
- ifstream DataFile
- DataFile.open(FileName)
- if (DataFile.fail())
- cout ltlt "Unable to read from " ltlt FileName ltlt
endl - return
-
- DataFile gtgt value
- while(!DataFile.eof())
- cout ltlt value ltlt endl
- DataFile gtgt value
-
- DataFile.close()
23Example 3
- Write n strings to a data file.
- include ltiostream.hgt
- include ltfstream.hgt
- void Write_String_File(int n)
- void main ()
- Write_String_File(5)
24Example 3
- void Write_String_File(int n)
- int i
- char FileName20 "Data.txt"
- ofstream DataFile
- DataFile.open(FileName)
- if (DataFile.fail())
- cout ltlt "Unable to write to " ltlt FileName ltlt
endl - return
-
- for (i0 iltn i)
- DataFile ltlt "Test String " ltlt i ltlt endl
- DataFile.close()
25Example 4
- Read strings from a data file.
- include ltiostream.hgt
- include ltfstream.hgt
- void Read_String_File()
- void main ()
- void Read_String_File()
26Example 4
- void Read_String_File()
- char buffer100
- char FileName20 "Data.txt"
- ifstream DataFile
- DataFile.open(FileName)
- if (DataFile.fail())
- cout ltlt "Unable to read from " ltlt FileName ltlt
endl - return
-
- DataFile.getline(buffer,sizeof(buffer))
- while(!DataFile.eof())
- cout ltlt buffer ltlt endl
- DataFile.getline(buffer,sizeof(buffer))
-
- DataFile.close()
27Example 5
- Append test scores to a data file
28(No Transcript)
29Contents of scores.dat
30Summary
- Components of file access
- Creating file objects
- How to open a sequential access data file
- How to test if a file opened successfully
- How to write records to a sequential file
- How to read records from a sequential file
- How to find the end of a file
- How to close a sequential access data file
31Example
- Average score function
- Write a function that reads scores stored in the
scores.dat file, calculates the average score,
and returns the average
scores.dat
97.2 87 73 84.5 72.3
32(No Transcript)