120 likes | 254 Views
Chapter 15.1 – Files for input and output. Let’s cut down on keyboard input! Can we save the output?. Definitions. File (data file) – A collection of data, stored under a common name, on a storage medium other than main memory Examples: C++ programs, Saved messages
E N D
Chapter 15.1 – Files for input and output Let’s cut down on keyboard input! Can we save the output?
Definitions • File (data file) – A collection of data, stored under a common name, on a storage medium other than main memory • Examples: C++ programs, Saved messages • File Stream – a one-way transmission path, used to connect a file stored on a device (like a disk) to a program • Mode – determines the direction of data on the transmission path • Input file stream reads data from a file • Output file stream writes data to a file
Input and Output file streams Program Disk #include <fstream> int main () ( return 0; } Input file stream file Output file stream
File Stream Objects and Methods • If you want to use a file for output, declare a variable of ofstream type • ofstream – output file streams • ofstream Outfile; • ofstream Out; • If you want to use a file for input, declare a variable of ifstream type • ifstream – input file streams • ifstream Infile; • ifstream In; • Any variable name can be used as long as it conforms to C++’s identifier rules
File Stream methods • Prewritten functions • Connecting a stream to an external file name – opening a file • Infile.open (“Student.data”, ios :: in); • Determining whether a successful connection has been made • if (Infile.fail( )) … • Closing a connection • Infile.close ( );
Syntax for Output Files • #include <ofstream> • Declare Outfile as an object of type fstream: • ofstream Outfile; • Open the external file with the statement: • Outfile.open (“Report.out”,ios::out); • Outfile is name in program, Report.out is external filename • Write to the file: • Outfile << “Grade:\t” << grade; • (writes to the file instead of the screen) • use Outfile instead of cout (start/debug with cout) • Close the file: • Outfile.close();
Syntax for Input Files • #include <ifstream> • Declare Infile as an object of type ifstream: • fstream Infile; • Open the external file with the statement: • Infile.open (“Student.data”,ios::in); • Infile - name in program, Student.data - external filename • Read from the file: • Infile >> grade; • (reads from the file instead of from the keyboard) • use Infile instead of cin (Make sure file exists!) • Close the file: • Infile.close();
ios :: in ios :: out ios :: app ios :: ate ios :: binary ios :: trunc ios :: nocreate ios :: noreplace open in input mode open in output mode open in append mode go to end of opened file open in binary mode (default, text) Delete file contents, if it exists If file does not exist, open fails If file exists, open for output fails Mode Indicators
Same file for input and output #include <iostream> #include <fstream> Using namespace std; main (){ fstream file1; int HW, newgrade; System(“clear”); /* Open the file for input */ file1.open ("grades.dat", ios::in); cout << "Homework total is "; file1 >> HW; // Print score that was read from file cout << HW << '\n'; file1.close (); // Close input file
Keep Value Around Between Runs /* Add score from keyboard */ cout << "What is score to be added?"; cin >> newgrade; cout << '\n'; HW += newgrade; cout << "Homework total is now "; cout << HW << '\n'; /* Reopen the file for output */ file1.open ("grades.dat", ios::out); file1 << HW; file1.close(); return 0; }//Run it again, new score is there!
Read until End of File (EOF) void main (void){ fstream TestFile; TestFile.open ("loop.file", ios::in); int TestScore, ID, Zip; // Read first record TestFile >> TestScore >> ID >> Zip; while ( TestFile.eof() == 0){ //same as (! TestFile.eof() ) // Process record Cout << TestScore << ID << Zip; // Read next record TestFile >> TestScore >> ID >> Zip; } // end while TestFile.close();} // end main