28-FileIO - #include <iostream> #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <fstream> #include <string> //http://www.ozzu.com/ftopic20781.html using namespace std; class Stuff { private: int data; public: Stuff(int d = 0) { data = d; } void print() { cout << "Our stuff is: " << data << endl; } }; int main (void) { cout << "Welcome to files. So glad that you have come.\n"; string divider; divider.assign(79, '*'); divider.append("\n"); divider.append("*\t\t\tSection Break\n"); divider.append(79, '*'); // // The three file stream objects: // ifstream - Reading from file (input) // ofstream - Writing to file (output) // fstream - Read/Write to file cout << "Using the >> operator\n"; string str; fstream input("c:\\test.txt", ios::in); while(input >> str) { cout << str << endl; } input.close(); // // Always good to remember to close the file stream // the deconstructor of the file object will close it // for you anyways, but why delay? If we crash before // the object can fall out of scope, it is possible // that the lock on the resource could still be // floating around. cout << divider << "\n\nUsing getline from <string>\n"; ifstream finput("c:\\test.txt"); // getline // istream& getline (istream& is, string& str, char delim = '\n'); // // Reads characters from an input stream into a string,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// stopping when one of the following things happens: // An end-of-file condition occurs on the input stream // When the maximum number of characters that can fit // into a string have been read // When a character read in from the string is equal to the // specified delimiter (newline is the default delimiter); the // delimiter character is removed from the input stream, but // not appended to the string. // The return value is a reference to the input stream. If // the stream is tested as a logical value (as in an if or // while), it is equivalent to true if the read was // successful and false otherwise (e.g., end of file). // cout << "finput's eof flag is: " << finput.eof() << endl; while(getline(finput, str, '\n')) { cout << str << endl; } cout << "finput's eof flag is: " << finput.eof() << endl; finput.close(); cout << divider << "\n\nUsing stream.getline, with open method\n"; char buffer[1000]; finput.open("c:\\test.txt"); while(!finput.eof()) { cout << "You cannot see me. Because open does not\n"
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Page1 / 6

28-FileIO - #include &lt;iostream&gt; #include...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online