Input/ Output Streams

Total Page:16

File Type:pdf, Size:1020Kb

Input/ Output Streams

Input/ Output Streams

// writes formatted output to a file, using <<

#include // 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

ofstream outfile("fdata.txt"); // create ofstream object

outfile << ch // insert (write) data << j << ' ' // needs space between numbers << d << str1 << ' ' // needs spaces between strings << str2; }

// reads formatted output from a file, using >>

#include const int MAX = 80; void main() { char ch; int j; double d; char str1[MAX]; char str2[MAX];

ifstream infile("fdata.txt"); // create ifstream object extract (read) data from it infile >> ch >> j >> d >> str1 >> str2;

cout << ch << endl // display the data << j << endl << d << endl << str1 << endl << str2 << endl; } // file output with strings #include // 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"; }

// file input with strings #include // 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 // for file functions #include // for strlen() void main() { char str[] = "Time is a great teacher, but unfortunately " "it kills all its pupils. Berlioz";

ofstream outfile("TEST.TXT"); // create file for output for(int j=0; j

// file input with characters #include // 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 } }

// binary input and output with integers

#include // for file streams const int MAX = 100; // size of buffer int buff[MAX]; // buffer for integers void main() { for(int j=0; j

for(j=0; j

for(j=0; j

// saves person object to disk #include // 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 }

// reads person object from disk #include // 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 // 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 );

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'

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

#include // for file streams #include // for exit() const int MAX = 1000; int buff[MAX]; void main() { for(int j=0; j

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); }

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

for(j=0; j

ifstream is; // create input stream is.open("a:edata.dat", ios::binary); if(!is) { cerr << "\nCould not open input file"; exit(1); }

cout << "\nReading..."; // read file is.read( (char*)buff, MAX*sizeof(int) ); if(!is) { cerr << "\nCould not read from file"; exit(1); }

for(j=0; j // 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

Recommended publications