Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS149D Elements of Computer Science

Similar presentations


Presentation on theme: "CS149D Elements of Computer Science"— Presentation transcript:

1 CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 21: 11/12/2002 Lecture 21: 11/12/2002 CS149D Fall 2002

2 Outline Reading and writing data files (section 3.6)
Lecture 21: 11/12/2002 CS149D Fall 2002

3 Data 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 <fstream.h> #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”,ios::in); //associate outputfile with file on disk “output.txt” in write mode (file is overwritten if already exists) outputfile.open(“output.txt”,ios::out); //shorthand form fstream inputfile (“input.txt”,ios::in); Lecture 21: 11/12/2002 CS149D Fall 2002

4 Data Files2/2 Alternatively can use
ifstream inputfile; //input file stream ofstream outputfile; //output file stream inputfile.open(“input.txt); outputfile.open(“output.txt”); Lecture 21: 11/12/2002 CS149D Fall 2002

5 File 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”,ios::in); //read from input file inputfile >> time >> height; fstream outputfile; outputfile.open (“output.txt”,ios::out); // write to output files outputfile << time << height; //Close file after we are done inputfile.close(); Outputfile.close(); Lecture 21: 11/12/2002 CS149D Fall 2002

6 Reading 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 Lecture 21: 11/12/2002 CS149D Fall 2002

7 Reading Data Files2/4 Sample input file 3 1 200 2 300 3 400
# The number of records is stored as the first line in the file fstream valuesfile; //open file for input valuesfile.open("sensor1.dat",ios::in); //read number of data lines in input file valuesfile>>num_data_pts; //read the first data line valuesfile>>id>>value; //for loop to read remaining data values from file for (int k=2; k <=num_data_pts;k++) { valuesfile >> id >> value; //process values read from file } Sample input file 3 1 200 2 300 3 400 Lecture 21: 11/12/2002 CS149D Fall 2002

8 Reading Data Files3/4 Sample input file 1 200 2 300 3 400 -1 -1
// 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",ios::in); //read the first data line valuesfile>>id>>value; //while loop to read remaining data lines while (id >= 0) { //process the current input value …. num_data_pts++; //read next line valuesfile >> id >> value; } Sample input file 1 200 2 300 3 400 -1 -1 Lecture 21: 11/12/2002 CS149D Fall 2002

9 Reading Data Files4/4 Sample input file 1 200 2 300 3 400
//end-of-file checked by using eof function int num_data_pts = 0, id; fstream valuesfile; //open file for input valuesfile.open("sensor.dat",ios::in); //read the first data line valuesfile>>id>>value; //while loop to read remaining data lines while (!valuesfile.eof()) { //process the current input value …. num_data_pts++; //read next line valuesfile >> id >> value; } Sample input file 1 200 2 300 3 400 Lecture 21: 11/12/2002 CS149D Fall 2002


Download ppt "CS149D Elements of Computer Science"

Similar presentations


Ads by Google