Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— 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”; File << “Data”; Inserts “Data” into file at current location Inserts “Data” into file at current location File >> data; File >> data; Extracts from File into data – hopefully the type matches. Extracts from File into data – hopefully the type matches. These files can be passed to our overloaded > 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; ifstream infile; infile.open(“someinputfile.txt”,ios::in); infile.open(“someinputfile.txt”,ios::in); int x; int x; infile >> x; infile >> x; infile.close(); 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. 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. 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. 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 Go to beginning of infile + offset infile.seekg(int offset,ios::cur) Move from current location by offset Move from current location by offset infile.seekg(int offset,ios::end) Go to end of infile + offset Go to end of infile + offset int infile.tellg() Returns offest from beginning of infile Returns offest from beginning of infileinfile.close() bool infile.good() Return true if the last read was ok Return true if the last read was ok bool infile.eof() Return true if we are at the end of the file Return true if we are at the end of the file

6 Writing Files ofstream outfile; ofstream outfile; outfile.open(“someoutputfile.txt”,ios::out); outfile.open(“someoutputfile.txt”,ios::out); string x(“stuff I am writing to a file”); string x(“stuff I am writing to a file”); outfile << x; outfile << x; outfile.close(); 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()” 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::beg) outfile.seekp(int offset, ios::cur) outfile.seekp(int offset, ios::cur) outfile.seekp(int offset, ios::end) outfile.seekp(int offset, ios::end) outfile.tellp() 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. 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. 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). Data is written as binary rather than text (for generating non-text files).

9 Output Example #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: 1234567

10 Input 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: 1213562342376 User Input: 12 Output:13142568246358888


Download ppt "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,"

Similar presentations


Ads by Google