100 likes | 104 Views
Learn how to read from and write to files in C++. Explore different methods for reading and writing, as well as positioning within a file. Find out how to handle unexpected input.
E N D
File I/O Supplemental Material
Background • In C++, files can be manipulated in the same manner we manipulate streams such as: cout and cin. • Therefore, the same conventions apply. • File << “Data”; • Inserts “Data” into file at current location • File >> data; • Extracts from File into data – hopefully the type matches. • These files can be passed to our overloaded << and >> operators in order to function on files instead of cout/cin.
Reading Files • Unlike cout/cin we need to explicitly declare how we use our file I/O. • ifstream infile; • infile.open(“someinputfile.txt”,ios::in); • int x; • infile >> x; • infile.close(); • The above code opens a file and reads from the beginning of the file (until whitespace, treating as an int) into x. • Unless we used ios::binary, you can think of the file similarly to someone at the keyboard typing in the equivalent input as what is in the file.
Reading Files - Methods • infile.get(char [] storage, int amount, char condition) • Gets “amount” characters and puts them into “storage”. Stops if “condition” is seen. • getline(istream & in, string target, char condition) • Gets characters from “in” and puts them in “target”. Stops when “condition” or end of file occurs. • infile.ignore(int amount, char condition) • Skip over “amount” characters, including (but stopping after) “condition”. get and getline stop at “condition”, so ignore is helpful.
Reading Files - Methods • infile.seekg(int offset,ios::beg) • Go to beginning of infile + offset • infile.seekg(int offset,ios::cur) • Move from current location by offset • infile.seekg(int offset,ios::end) • Go to end of infile + offset • int infile.tellg() • Returns offest from beginning of infile • infile.close() • bool infile.good() • Return true if the last read was ok • bool infile.eof() • Return true if we are at the end of the file
Writing Files • ofstream outfile; • outfile.open(“someoutputfile.txt”,ios::out); • string x(“stuff I am writing to a file”); • outfile << x; • outfile.close(); • The above code opens a file writes the string x into the file. • If you replaced all of our cout statements with an ofstream variable all your output would go to the file instead of the screen. • This is why our overloaded << takes in an ostream. It won’t always be cout, it could be access to a file for writing.
Writing Files - Methods • outfile.write(char [] towrite, int amount) • Writes “amount” characters from “towrite” into outfile. Remember, you can use a “string” by using “.c_str()” • Position Methods: Same as reading • outfile.seekp(int offset, ios::beg) • outfile.seekp(int offset, ios::cur) • outfile.seekp(int offset, ios::end) • outfile.tellp()
Writing Files - Methods • outfile.close() • outfile.open(char [] outputfilename,ios::out) • Opens “outputfilename”, writes occur at the beginning of the file and are destructive. • outfile.open(char [] outputfilename,ios::app) • All writes are appended to the end of the file. No pre-existing data is lost. • outfile.open(char [] outputfilename,ios::binary) • Data is written as binary rather than text (for generating non-text files).
#include<iostream> #include<fstream> using namespace std; int main() { ofstream outfile; outfile.open(“outputfile.txt”,ios::out); cout << “Count to what?” << endl; int num; cin >> num; for(int i=1;i<=num;i++) { outfile << i << endl; } outfile.close(); return 0; } User Input: 7 Contents of outputfile.txt: 1 2 3 4 5 6 7 Output Example
#include<iostream> #include<fstream> using namespace std; int main() { ifstream infile; infile.open(“inputfile.txt”,ios::in); cout << “Add what to each value in the file?” << endl; int num; cin >> num; while(!infile.eof()) { int temp; infile >> temp; cout << temp + num << endl; } infile.close(); return 0; } Contents of inputfile.txt: 1 2 13 56 234 23 76 User Input: 12 Output: 13 14 25 68 246 35 88 88 Input Example Not exactly what we expected is it?