1 / 12

Chapter 15.1 – Files for input and output

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

ivi
Download Presentation

Chapter 15.1 – Files for input and output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 15.1 – Files for input and output Let’s cut down on keyboard input! Can we save the output?

  2. 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

  3. Input and Output file streams Program Disk #include <fstream> int main () ( return 0; } Input file stream file Output file stream

  4. 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

  5. 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 ( );

  6. 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();

  7. 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();

  8. 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

  9. 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

  10. 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!

  11. 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

More Related