File IO Streams for Input and Output output

  • Slides: 20
Download presentation
File I/O

File I/O

Streams for Input and Output output data input data keyboard executing program disk file

Streams for Input and Output output data input data keyboard executing program disk file input stream output stream screen

Reading Input From Files & Other Input Options • Two main types of files:

Reading Input From Files & Other Input Options • Two main types of files: text and binary • Text file: comprised of ASCII characters examples ? files you type with Eclipse; files you type with pico, simpletext, notepad* • Binary file: structure/contents determined by program which wrote it examples ? MSWord files, jpg, mp 3

end of line marker Example Text File int main ( ) <eoln> { <eoln>

end of line marker Example Text File int main ( ) <eoln> { <eoln> <tab> char ch; <eoln> <tab> ch = 'X’; <eoln> <tab> cout << ch; <eoln> <tab> return (0); <eoln> } <eoln> <eof> end of file marker

Performing Basic Text file I/O • #include <fstream> – contains data types ifstream, ofstream,

Performing Basic Text file I/O • #include <fstream> – contains data types ifstream, ofstream, and functions open and close 1) Declare a file stream variable to represent the file you intend to use 2) Open the file you intend to use, linking it to the file stream variable 3) Use new input file stream to read, and/or use new output file stream to write 4) Close the file when done with it

What does opening a file do? • associates the C++ identifier for your file

What does opening a file do? • associates the C++ identifier for your file with the physical (disk) name for the file • Forms a link between the two! • if the input file does not exist on disk, open is not successful • if the output file does not exist on disk, a new file with that name is created • if the output file already exists, it is erased! • places a file reading marker at the very beginning of the file, pointing to the first character in it

Doing Input and Output #include <fstream> input data output data disk file “C: my.

Doing Input and Output #include <fstream> input data output data disk file “C: my. Infile. txt” executing program your variable (of type ifstream) disk file “C: my. Out. txt” your variable (of type ofstream)

Performing Disk Input and Output • #include <fstream> – contains data types ifstream, ofstream,

Performing Disk Input and Output • #include <fstream> – contains data types ifstream, ofstream, and functions open and close 1) Declare a file stream variable to represent the file you intend to use • Declare one for each direction 2) Open the file you intend to use, linking it to the file stream variable 3) Use new input file stream to read, and/or use new output file stream to write 4) Close the file when done with it

The Only Disk I/O Statements You’ll Ever Need #include <fstream> ifstream ofstream my. Infile;

The Only Disk I/O Statements You’ll Ever Need #include <fstream> ifstream ofstream my. Infile; my. Outfile; // declarations my. Infile. open(“A: \my. In. txt”); // open files my. Outfile. open(“A: \my. Out. txt”); : : my. Infile. close( ); // close files my. Outfile. close( );

Stream input/output ios istream ifstream ostream iostream fstream ofstream

Stream input/output ios istream ifstream ostream iostream fstream ofstream

Common Error: read past <eof> marker more reads than data in. File >> i

Common Error: read past <eof> marker more reads than data in. File >> i >> j >> ch >> k; effect in. File would enter fail state all subsequent attempts to read from in. File would be skipped i, j, and ch would be as before, k would be garbage

Another Common Error: file does not open • file name spelled incorrectly in. File.

Another Common Error: file does not open • file name spelled incorrectly in. File. open ( "data. text" ); or, if file is not on the disk • effect • in. File would enter fail state; • all subsequent attempts to read from in. File would be skipped

Stream Fail State • possible reasons for entering fail state include: • invalid input

Stream Fail State • possible reasons for entering fail state include: • invalid input data (often the wrong type) • opening an input file that doesn’t exist • opening an output file on a diskette that is already full or is write-protected • when a stream enters the fail state, further I/O operations using that stream have no effect at all. • The computer does not automatically halt the program or give any error message

Example in. Stream. open(”stuff. dat”); if (in. Stream. fail()) // or if (!in. Stream.

Example in. Stream. open(”stuff. dat”); if (in. Stream. fail()) // or if (!in. Stream. good()) { //will get here if file does not exist cout << “Input file opening failed. n”; }

I/O states • consists of 3 bits (boolean values) T F eof fail eof()

I/O states • consists of 3 bits (boolean values) T F eof fail eof() – nonzero value if eofbit is set fail() – nonzero if badbit or failbit is set bad() – nonzero if badbit is set good() – value of 0 (no bits set) F bad

What they mean in. File. eof(): true if eof symbol has been read in.

What they mean in. File. eof(): true if eof symbol has been read in. File. fail(): true if operation failed in. File. bad(): true if stream is corrupt in. File. good(): true if no io_flag bits were set, i. e. , in. File. good() = !in. File. eof() && !in. File. fail() && !in. File. bad() in. File. clear(); //use this to clear all the bad bits!

When is eof() set? test. dat #include <fstream> 3<eof> int main() { ifstream in.

When is eof() set? test. dat #include <fstream> 3<eof> int main() { ifstream in. Data; int val 1, val 2; in. Data. open(“test. dat”); in. Data >> val 1; //eof() will be false (0) in. Data >> val 2; //eofbit is set & eof() returns true

Read a file name and then open • we use two types of "strings":

Read a file name and then open • we use two types of "strings": C++ standard library and C-style string file. Name; cout << "Enter file name -> "; cin >> file. Name; in. File. open ( file. Name. c_str( ) ); convert to C-style string

Using a loop to check Input cout << “Please enter your input file name:

Using a loop to check Input cout << “Please enter your input file name: ”; cin >> my. File. Name; in. File. open(my. File. Name. c_str()); //convert to c_string and opens file while (!in. File. good()) // loop as long as the input isn’t good { in. File. clear(); //puts the file states back to “good” cout << endl << “Invalid file name. Try again: ”; cin >> my. File. Name; in. File. open(my. File. Name. c_str()); }

Useful Experiment • Take any of your previous working labs • Modify it to

Useful Experiment • Take any of your previous working labs • Modify it to take its input from a file • Have it check the file stream status for bad input data! • Modify it to put its output in another file • You can get help from the handouts! (don’t forget to close your files!)