Title: CS149D Elements of Computer Science
1CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer
Science Old Dominion University Lecture 21
11/12/2002
2Outline
- Reading and writing data files (section 3.6)
3Data Files1/2
- Reading or writing large amounts of data
- Results from scientific or engineering
experiments usually are stored in data files.
These data files are later processed to extract
desired information
include ltfstream.hgt information related to
manipulating a file fstream inputfile,outputfile
declare two file streams //associate inputfile
with file on disk input.txt in read mode
inputfile.open (input.txt,iosin) //associate
outputfile with file on disk output.txt in
write mode (file is overwritten if already
exists) outputfile.open(output.txt,iosout) //
shorthand form fstream inputfile
(input.txt,iosin)
4Data Files2/2
Alternatively can use ifstream inputfile //inpu
t file stream ofstream outputfile //output file
stream inputfile.open(input.txt) outputfile.ope
n(output.txt)
5File I/O
Once file object have been successfully
associated with file, can read or write
information the same as reading from the
keyboard, or writing to the monitor
fstream inputfile inputfile.open
(input.txt,iosin) //read from input
file inputfile gtgt time gtgt height
fstream outputfile outputfile.open
(output.txt,iosout) // write to output
files outputfile ltlt time ltlt height
//Close file after we are done inputfile.close()
Outputfile.close()
6Reading Data Files1/4
- File consists of a number of lines (records)
- Each line has one or more fields to be read by
the program - File Structure Alternatives
- The number of records (lines) in the file is
known, usually stored as the first line in the
file ? use a for loop to read the information - The last line in the file contains a special data
value (usually not occurring in the allowable
data values) ? use a while loop with a condition
that is true as long as the data value read is
not the special data value - A special end-of-file indicator is inserted at
the end of every file. We use the function eof to
detect the end-of-file indicator
7Reading Data Files2/4
The number of records is stored as the first
line in the file fstream valuesfile //open file
for input valuesfile.open("sensor1.dat",iosin)
//read number of data lines in input
file valuesfilegtgtnum_data_pts //read the first
data line valuesfilegtgtidgtgtvalue //for loop to
read remaining data values from file for (int
k2 k ltnum_data_ptsk) valuesfile gtgt id gtgt
value //process values read from file
Sample input file 3 1 200 2 300 3 400
8Reading Data Files3/4
// input file terminated with a negative id value
in last line int num_data_pts 0, id fstream
valuesfile //open file for input valuesfile.open(
"sensor_trailer.dat",iosin) //read the first
data line valuesfilegtgtidgtgtvalue //while loop to
read remaining data lines while (id gt
0) //process the current input
value . num_data_pts //read next
line valuesfile gtgt id gtgt value
Sample input file 1 200 2 300 3 400 -1 -1
9Reading Data Files4/4
//end-of-file checked by using eof function int
num_data_pts 0, id fstream valuesfile //open
file for input valuesfile.open("sensor.dat",iosi
n) //read the first data line valuesfilegtgtidgtgtval
ue //while loop to read remaining data
lines while (!valuesfile.eof()) //process the
current input value . num_data_pts //read
next line valuesfile gtgt id gtgt value
Sample input file 1 200 2 300 3 400