1 / 10

File I/O

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;

robert-barr
Download Presentation

File I/O

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. File I/O Supplemental Material

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

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

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

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

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

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

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

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

  10. #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?

More Related