INTRO TO COMPUTER SCIENCE II
FILE INPUT/OUTPUT
CS162
File I/O
What type of I/O have we used before?
File input output
Allows us to read and write data to files for long term storage
File I/O
What type of I/O have we used before? Command line I/O
Uses predefined stream objects and classes to control the flow of characters cin – input stream, defaults to keyboard
cout – output stream, defaults to terminal
cerr – output stream, for errors
File input output
Allows us to read and write data to files for long term storage
File I/O
File streams – derived from iostream, #include
ifstream
Input only file stream
ofstream
Output only file stream
fstream
Input/output file stream Most common
File I/O
Unlike normal I/O streams, file streams need to be explicitly set up Create stream objects before we can use them
General algorithm
1) Create the file object
2) Open the file
3) Perform an action on the file (like read/write) 4) Close the file
File I/O – Create file object
Instantiate an object of the appropriate class #include
int main(){
fstream f;
ifstream fin;
ofstream fout;
return 0; }
File I/O – Open the file
Use filename as parameter, mode optional Syntax: open(filename, mode)
2 ways
fstream f(“file.txt”);
fstream f;
f.open(“file.txt”)
f.open(“file.txt”, ios::in);
Modes
ios::in – open file for input
ios::out – open for output
ios::binary – open file in binary mode
ios::ate – opens a file and puts the output position “at the end” ios::app – opens a file and appends to the end
ios::trunc – deletes the existing file contents
File I/O – Modes
Default modes – used when you don’t specify a mode ifstream – ios::in
ofstream – ios::out
fstream – ios::out|ios::in
If you use another mode than the default, you have to explicitly write all modes out
Modes can be combined using bitwise OR operator Not all combinations are valid
f.open(“file.txt”, ios::out|ios::in|ios::app);
File I/O – Errors with opening files
When will a file not open?
If a stream already has a file open, you can’t open another Some combinations of modes (app & trunc)
How do you check?
if (f.is_open()){ //f.fail() //do stuff
}else{
cout << “Error opening file!” << endl;
}
File I/O – Perform action on the file
Two actions Reading
Assume the file empty
Read whole file usingwhile(!eof()){} Read single character using get()
Read an entire line using getline()
Writing
Need to be aware of where the cursor is
int num=0;
fstream f;
f.open(“nums.txt”)
f >> num;
int num=0;
fstream f;
f.open(“file.txt”)
f << “This is a file.” << endl;
File I/O – Close the file
Don’t forget!
Use stream member function close
f.close();
Once closed, stream object can be re-used to open another file
f.open(“file1.txt”);
f.close();
f.open(“file2.txt”);
Parsing Files
Files can separate data in many ways What are some common ones?
Parsing Files
Files can separate data in many ways
What are some common ones? Newline character \n
Comma ,
Tab character
Colon :
Space “ “
Parsing Files
File input
Space
Default separator when using “>>” getline()
Commas
Use getline(cin, str, ‘,’);
Discards specified characters (aka ‘,’) Reads text until it hits the character
Parsing Files
Newline
Most reader friendly files use this Often used for “next entry”
Football,basketball,soccer
Coffee,soda,water
istream
fin.ignore(n, delimiter) fin.ignore(“\n”)
Parsing Files
File output
You control delimiters Easier to handle
fout << “Hello world!” << endl;