<p>Input/ Output Streams</p><p>// writes formatted output to a file, using <<</p><p>#include <fstream.h> // for file I/O (includes iostream.h) void main() { char ch = 'x'; int j = 77; double d = 6.02; char str1[] = "Kafka"; // strings without char str2[] = "Proust"; // embedded spaces</p><p> ofstream outfile("fdata.txt"); // create ofstream object</p><p> outfile << ch // insert (write) data << j << ' ' // needs space between numbers << d << str1 << ' ' // needs spaces between strings << str2; }</p><p>// reads formatted output from a file, using >></p><p>#include <fstream.h> const int MAX = 80; void main() { char ch; int j; double d; char str1[MAX]; char str2[MAX];</p><p> ifstream infile("fdata.txt"); // create ifstream object extract (read) data from it infile >> ch >> j >> d >> str1 >> str2;</p><p> cout << ch << endl // display the data << j << endl << d << endl << str1 << endl << str2 << endl; } // file output with strings #include <fstream.h> // for file functions void main() { ofstream outfile("TEST.TXT"); // create file for output // send text to file outfile << "I fear thee, ancient Mariner!\n"; outfile << "I fear thy skinny hand\n"; outfile << "And thou art long, and lank, and brown,\n"; outfile << "As is the ribbed sea sand.\n"; }</p><p>// file input with strings #include <fstream.h> // for file functions void main() { const int MAX = 80; // size of buffer char buffer[MAX]; // character buffer ifstream infile("TEST.TXT"); // create file for input while( !infile.eof() ) // until end-of-file { infile.getline(buffer, MAX); // read a line of text cout << buffer << endl; // display it } } // file output with characters #include <fstream.h> // for file functions #include <string.h> // for strlen() void main() { char str[] = "Time is a great teacher, but unfortunately " "it kills all its pupils. Berlioz";</p><p> ofstream outfile("TEST.TXT"); // create file for output for(int j=0; j<strlen(str); j++) // for each character, outfile.put(str[j]); // write it to file }</p><p>// file input with characters #include <fstream.h> // for file functions void main() { char ch; // character to read ifstream infile("TEST.TXT"); // create file for input while( infile ) // read until EOF { infile.get(ch); // read character cout << ch; // display it } }</p><p>// binary input and output with integers</p><p>#include <fstream.h> // for file streams const int MAX = 100; // size of buffer int buff[MAX]; // buffer for integers void main() { for(int j=0; j<MAX; j++) // fill buffer with data buff[j] = j; // (0, 1, 2, ...) ofstream os("edata.dat", ios::binary); // create output stream os.write( (char*)buff, MAX*sizeof(int) ); // write to it os.close(); // must close it</p><p> for(j=0; j<MAX; j++) // erase buffer buff[j] = 0; ifstream is("edata.dat", ios::binary); // create input stream is.read( (char*)buff, MAX*sizeof(int) ); // read from it</p><p> for(j=0; j<MAX; j++) // check data if( buff[j] != j ) { cerr << "\nData is incorrect"; return; } cout << "\nData is correct"; } Reading and Writing Objects</p><p>// saves person object to disk #include <fstream.h> // for file streams class person // class of persons { protected: char name[40]; // person's name int age; // person's age public: void getData(void) // get person's data { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; } }; void main(void) { person pers; // create a person pers.getData(); // get data for person // create ofstream object ofstream outfile("PERSON.DAT", ios::binary); outfile.write( (char *)&pers, sizeof(pers) ); // write to it }</p><p>// reads person object from disk #include <fstream.h> // for file streams class person // class of persons { protected: char name[40]; // person's name int age; // person's age public: void showData(void) // display person's data { cout << "\n Name: " << name; cout << "\n Age: " << age; } }; void main(void) { person pers; // create person variable ifstream infile("PERSON.DAT", ios::binary); // create stream infile.read( (char*)&pers, sizeof(pers) ); // read stream pers.showData(); // display person } // reads and writes several objects to disk #include <fstream.h> // for file streams class person // class of persons { protected: char name[40]; // person's name int age; // person's age public: void getData(void) // get person's data { cout << "\n Enter name: "; cin >> name; cout << " Enter age: "; cin >> age; } void showData(void) // display person's data { cout << "\n Name: " << name; cout << "\n Age: " << age; } }; void main(void) { char ch; person pers; // create person object fstream file; // create input/output file // open for append file.open("PERSON.DAT", ios::app | ios::out | ios::in | ios::binary );</p><p> do // data from user to file { cout << "\nEnter person's data:"; pers.getData(); // get one person's file.write( (char*)&pers, sizeof(pers) ); // write to file cout << "Enter another person (y/n)? "; cin >> ch; } while(ch=='y'); // quit on 'n'</p><p> file.seekg(0); // reset to start of file file.read( (char*)&pers, sizeof(pers) ); // read first person while( !file.eof() ) // quit on EOF { cout << "\nPerson:"; // display person pers.showData(); file.read( (char*)&pers, sizeof(pers) ); // read another } // person } // handles errors during input and output</p><p>#include <fstream.h> // for file streams #include <process.h> // for exit() const int MAX = 1000; int buff[MAX]; void main() { for(int j=0; j<MAX; j++) // fill buffer with data buff[j] = j;</p><p> ofstream os; // create output stream os.open("a:edata.dat", ios::trunc | ios::binary); // open it if(!os) { cerr << "\nCould not open output file"; exit(1); }</p><p> cout << "\nWriting..."; // write buffer to it os.write( (char*)buff, MAX*sizeof(int) ); if(!os) { cerr << "\nCould not write to file"; exit(1); } os.close(); // must close it</p><p> for(j=0; j<MAX; j++) // clear buffer buff[j] = 0;</p><p> ifstream is; // create input stream is.open("a:edata.dat", ios::binary); if(!is) { cerr << "\nCould not open input file"; exit(1); }</p><p> cout << "\nReading..."; // read file is.read( (char*)buff, MAX*sizeof(int) ); if(!is) { cerr << "\nCould not read from file"; exit(1); }</p><p> for(j=0; j<MAX; j++) // check data if( buff[j] != j ) { cerr << "\nData is incorrect"; exit(1); } cout << "\nData is correct"; } // person objects do disk I/O outfile.open("PERSON.DAT", ios::app | #include <fstream.h> // for file streams ios::binary); outfile.write( (char*)this, sizeof(*this) ); class person // class of persons // write to it { } protected: char name[40]; // person's name int person::diskCount() // return # of persons int age; // person's age { // in file public: ifstream infile; void getData(void) // get person's data infile.open("PERSON.DAT", ios::binary); { // go to 0 bytes from end cout << "\n Enter name: "; cin >> name; infile.seekg(0, ios::end); cout << " Enter age: "; cin >> age; // calculate number of persons } return (int)infile.tellg() / sizeof(person); void showData(void) // show person's data } { cout << "\n Name: " << name; void main(void) cout << "\n Age: " << age; { } person p; // make an empty person void diskIn(int); // read from file char ch; void diskOut(); // write to file static int diskCount(); // return number of do // save persons to disk // persons in file { }; cout << "\nEnter data for person:"; p.getData(); // get data void person::diskIn(int pn) p.diskOut(); // write to disk { cout << "Do another (y/n)? "; ifstream infile; // make stream cin >> ch; // open it } infile.open("PERSON.DAT", ios::binary); while(ch=='y'); // until user enters 'n' // move file ptr infile.seekg( pn*sizeof(person) ); // how many persons in file? // read one person int n = person::diskCount();cout << infile.read( (char*)this, sizeof(*this) ); "\nThere are " << n << " persons in file"; } for(int j=0; j<n; j++) // for each one, { void person::diskOut() cout << "\nPerson #" << j; // write person to end of file p.diskIn(j); // read person from disk { p.showData(); // display person ofstream outfile; // make stream } // open it }</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages7 Page
-
File Size-