Download presentation
Presentation is loading. Please wait.
1
File I/O
2
Streams for Input and Output
output data input data executing program keyboard disk file screen input stream output stream
3
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, mp3
4
Example Text File end of line marker int main ( ) { char ch; ch = 'X’;
cout << ch; return (0); } <eoln> <eoln> <tab> <eoln> <tab> <eoln> <tab> <eoln> <tab> <eoln> <eoln> end of file marker <eof>
5
Performing Basic Text file I/O
#include <fstream> contains data types ifstream, ofstream, and functions open and close Declare a file stream variable to represent the file you intend to use Open the file you intend to use, linking it to the file stream variable Use new input file stream to read, and/or use new output file stream to write Close the file when done with it ---
6
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
7
Doing Input and Output your variable (of type ifstream)
#include <fstream> your variable (of type ifstream) (of type ofstream) disk file “C:\myInfile.txt” “C:\myOut.txt” executing program input data output data
8
Performing Disk Input and Output
#include <fstream> contains data types ifstream, ofstream, and functions open and close Declare a file stream variable to represent the file you intend to use Declare one for each direction Open the file you intend to use, linking it to the file stream variable Use new input file stream to read, and/or use new output file stream to write Close the file when done with it ---
9
The Only Disk I/O Statements You’ll Ever Need
#include <fstream> ifstream myInfile; // declarations ofstream myOutfile; myInfile.open(“A:\\myIn.txt”); // open files myOutfile.open(“A:\\myOut.txt”); : myInfile.close( ); // close files myOutfile.close( );
10
ifstream iostream ofstream
Stream input/output ios istream ostream ifstream iostream ofstream fstream
11
Common Error: read past <eof> marker
more reads than data inFile >> i >> j >> ch >> k; effect inFile would enter fail state all subsequent attempts to read from inFile would be skipped i, j, and ch would be as before, k would be garbage ---
12
Another Common Error: file does not open
file name spelled incorrectly inFile.open ( "data.text" ); or, if file is not on the disk effect inFile would enter fail state; all subsequent attempts to read from inFile would be skipped ---
13
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
14
Example inStream.open(”stuff.dat”); if (inStream.fail()) // or if (!inStream.good()) { //will get here if file does not exist cout << “Input file opening failed.\n”; }
15
I/O states T F F consists of 3 bits (boolean values) eof fail bad
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) T F F
16
inFile.clear(); //use this to clear all the bad bits!
What they mean inFile.eof(): true if eof symbol has been read inFile.fail(): true if operation failed inFile.bad(): true if stream is corrupt inFile.good(): true if no io_flag bits were set, i.e., inFile.good() = !inFile.eof() && !inFile.fail() && !inFile.bad() inFile.clear(); //use this to clear all the bad bits!
17
When is eof() set? test.dat 3<eof>
#include <fstream> int main() { ifstream inData; int val1, val2; inData.open(“test.dat”); inData >> val1; //eof() will be false (0) inData >> val2; //eofbit is set & eof() returns true test.dat 3<eof>
18
Read a file name and then open
we use two types of "strings": C++ standard library and C-style string fileName; cout << "Enter file name -> "; cin >> fileName; inFile.open ( fileName.c_str( ) ); convert to C-style string
19
Using a loop to check Input
cout << “Please enter your input file name:”; cin >> myFileName; inFile.open(myFileName.c_str()); //convert to c_string and opens file while (!inFile.good()) // loop as long as the input isn’t good { inFile.clear(); //puts the file states back to “good” cout << endl << “Invalid file name. Try again:”; }
20
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!)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.