CSCI26I
File I/O in Detail
CSCI26I File I/O in Detail ? Review Programs can read and write - - PowerPoint PPT Presentation
CSCI26I File I/O in Detail ? Review Programs can read and write files to disk And from any stream ! #include <fstream> ifstream my_file_object(filename.ext) Use while loops to repeatedly read from the stream
File I/O in Detail
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main() { char x; // or int x, double x, etc. ifstream mydata("FILENAME"); if(!mydata) {
} while(myfile >> x) { // do marvelous things } myfile.close(); return 0; }
1 2 200 300 1.1 2.1 x cc 320 420
datafile.txt
int main() { int x, y; ifstream data("data.txt"); while (data >> x >> y) { cout << x << " " << y << endl; } data.close(); system("PAUSE"); return 0; }
1 2 200 300 320 420
datafile.txt
420 320 300 200 2 1
ifstream numbaz(“datafile.txt”);
1 2 200 300 320 420
datafile.txt
420 320 300 200 2 1
numbaz int x; while (numbaz >> x) { // have fun }
cursor
420 320 300 200 2 1
numbaz int x; while (numbaz >> x) { // have fun }
cursor
whee!
myoutput << “obladee obladah life goes on”; myoutput.close();
cout << “i am the walrus”;
Mmmmoooooooooo!