I/O streams 1-24-2013
I/O streams file I/O end-of-file eof reading in strings Browse the following libraries at cplusplus.com String Library STL: Standard Template Library (vector) IOStream Library HW#2 posted due: Thursday, 1/31/13 Quiz today
vector<point> polygon; Point p1(100,200); Point p2(150,150); Point p3(150,50); Point p4(75,90); polygon.push_back(p1); polygon.push_back(p2); polygon.push_back(p3); polygon.push_back(p4); for (int i = 0; i < polygon.size(); i++) cout << polygon[i] << endl;
preface.txt -------------------------------------------------- 1 After a few computer science courses, students may start to 2 get the feeling that programs can always be written to 3 solve any computational problem. Writing the program may 4 be hard work. For example, it may involve learning a 5 difficult technique. And many hours of debugging. But 6 with enough time and effort, the program can be written. 7 8 So it may come as a surprise that this is not the case: 9 there are computational problems for which no program -------------------------------------------------- next previous open quit ------- command: o file: introduction.txt
void run_file_viewer() main function of the file viewer displays the given number of lines asks user what to do next: q o quit open a text file vector<string> v_document_lines data structure for storing the document s lines
void run_file_viewer() { vector<string> v_document_lines; string file_name; open_file(file_name, v_document_lines); while (true) { // while command is not 'quit' display(file_name, v_document_lines); cout << "command: "; char command = '-'; cin.get(command); cin.get(); // '\n' switch (command) { case 'q': return; case 'o': { open_file(file_name, v_document_lines); } // end case o } // end switch } // end while } // end run_file_viewer
void display(const string & file_name, const vector<string> & v_document_lines) { cout << endl << file_name << endl; string long_separator(50, '-'); cout << long_separator << endl; for (int i = 0; i < v_document_lines.size(); ++i) cout << setw(3) << i+1 << " " << v_document_lines[i] << endl; cout << long_separator << endl << " open quit" << endl; string short_separator(8, '-'); cout << short_separator << endl; } // end display
An input stream is a flow of characters from a source e.g. cin is of type istream cin >> n; // reads a value for n from the default input stream, the keyboard An output stream is a flow of characters to a destination e.g. cout is of type ostream cout << n // displays the value of n to the default output stream, the screen Use library: #include<iostream>
Another source for input could be a file; likewise another destination for output could be a file. #include <fstream>; ifstream in_data; // input file stream in_data.open( pointdata.txt ); /* opens the file (associates it with the text file named pointdata.txt) */ in_data >> a; // a s value is read from the file
int main() { vector<point> polygon; // creates an empty vector /* read in point data from a file and add to the vector */ ifstream in_data; int a; int b; in_data.open("pointdata.txt"); in_data >> a >> b; polygon.push_back( Point(a,b) ); in_data.close();
Tip: always check whether a file was opened successfully in_data.open("pointdata.txt"); if (in_data.fail()) { cout << "Failed to open input file." << endl; exit(1); // aborts the program with error code 1 }
Would like to read in any number of points /* assume input file contains any number of lines, with two integer values per line */ while (in_data >> a >> b) { polygon.push_back( Point(a,b) ); } in_data.close(); // display the polygon for (int i = 0; i < polygon.size(); i++) cout << polygon[i]; cout << endl;
char infilename[16]; // C string cout << "Enter the input file name << (maximum of 15 characters)" << endl; cin >> infilename; in_data.open(infilename); Example: polygon.cpp
vector<string> roster; // empty roster // pseudocode open the input file and check while( there is unread data in the input file ) { read in the name add to the roster }
ifstream in_data; in_data.eof() // not at end of file in_data.getline( str, n) ; // reads in a line of up to n characters into // the C string str string name; in_data.getline( name, 80 ) // fails
char cname[80]; // stores 79 chars plus \0 in_data.getline(cname, 80); // reads chars into cname until either // 79 chars have been read, or // reached the delimiter ( \n in this case) string name( cname ); // constructor for string can use C string
while(!in_data.eof()) { in_data.getline(cname,80); string name(cname); }; problem: the input file: Sam Jim Bob end of file Betty is really a stream: S a m \n J i m space B o b \n B e t t y \n
while(!in_data.eof()) { in_data.getline(cname,80); string name(cname); if (!name.empty()) roster.push_back(name); }; in_data.close(); for (int i = 0; i < roster.size(); i++) cout << roster[i] << endl; cout << "end of roster" << endl;
For Tuesday, read the following from Maciel: Chapter 3 section 3.3 and 3.4 on I/O streams and string streams. Chapter 4 on error checking Chapter 6 on dynamically allocated arrays