/* Date : 11th March 1999 */ /* This file writes from a text file and copies it into another text file. It also appends to a file. If this file does not exist it is created. it uses the file i/o stream operators ofstream and ifstream */ #include #include #include int main(void) { int mark; char name[100]; // Assuming that the strings in the file < 100 chars // open the files ifstream inputstream ("test.txt"); // reading if (!inputstream) { cerr << "Can't open test.txt" << endl; exit (1); } ofstream outputstream ("testout.txt"); // writing if (!outputstream) { cerr << "Can't open testout.txt" << endl; exit (2); } ofstream appendstream ("testapp.txt",ios::app); // append to file if (!appendstream) { cerr << "Can't open testapp.txt" << endl; exit (3); } cout << "Name \t Marks" << endl; outputstream << "Name \t Marks" << endl; // reading from input file inputstream >> name >> mark; while (!inputstream.eof()) { // writing to output file outputstream << name << " \t " << mark << endl; //writing to file open in append mode appendstream << name << " \t " << mark << endl; // writing to screen cout << name << " \t " << mark << endl; // reading from input file inputstream >> name >> mark; } // closing the files inputstream.close(); outputstream.close(); appendstream.close(); return 0; }