// fileinout1.cpp By: Aiman Hanna - ©2006 Aiman Hanna // This program illustrates how a program can read from an input file // and writes to an output file. // Key Points: 1) Physical file names and logical file names. // 2) ifstream and ofstream classes // 3) eof() function #include #include #include void ShowFile( char* infilename ) { // This function reads an input file line by line and shows the lines on the screen. // The input file is assumed to have lines formatted as follows: // string int double char, where are values are separated by tabs // For example: Mark 30 12.3 k // Martin 23 14.8 j char* st = new char[20]; int x; double d; char ch; ifstream fin; ofstream fout; fin.open( infilename ); // from this point, fin is the logical name of infilename if (!fin ) // if you can not open the file { cerr << " Could not open input file: " << infilename << endl; exit( 1 ); } // Read the first line from the input file, the following while will check // whether the input attempt was successful or not fin >> st >> x >> d >> ch; while ( !fin.eof() ) // As long as it is not the end of the input file yet { cout << st << "\t" << x << "\t" << d << "\t" << ch << endl; fin >> st >> x >> d >> ch; } // close the file before terminating fin.close(); } void CopyFile1( char* infilename, char* outfilename ) { // This function reads an input file and copies it to an output file // The input file is assumed to have lines formatted as follows: // string int double char, where are values are separated by tabs // For example: Mark 30 12.3 k // Martin 23 14.8 j char* st = new char[20]; int x; double d; char ch; ifstream fin; ofstream fout; fin.open( infilename ); // from this point, fin is the logical name of infilename if (!fin ) // if you can not open the file { cerr << " Could not open input file: " << infilename << endl; exit( 1 ); } fout.open( outfilename ); // from this point, fout is the logical name of outfilename if (!fout ) // if you can not open the file { cerr << " Could not open output file: " << outfilename << endl; fin.close(); // close the input file first. exit( 1 ); } // Read the first line of the input file fin >> st >> x >> d >> ch; while ( !fin.eof() ) // As long as it is not the end of the input file yet { fout << st << "\t" << x << "\t" << d << "\t" << ch << endl; fin >> st >> x >> d >> ch; } // close the file before terminating fin.close(); fout.close(); } void CopyFile2( char* infilename, char* outfilename ) { // This function reads an input file and copies it to an output file // The lines of the input file are formatted as follows: // The first line includes a date, which is day/month/year // The second line includes three double values // All following lines are formatted as follows: // string int double char, where are values are separated by tabs // For example: Mark 30 12.3 k // Martin 23 14.8 j char* st = new char[20]; int day, month, year, x; double d1, d2, d3, d; char ch, slash; ifstream fin; ofstream fout; fin.open( infilename ); // from this point, fin is the logical name of infilename if (!fin ) // if you can not open the file { cerr << " Could not open input file: " << infilename << endl; exit( 1 ); } fout.open( outfilename ); // from this point, fout is the logical name of outfilename if (!fout ) // if you can not open the file { cerr << " Could not open output file: " << outfilename << endl; fin.close(); // close the input file first. exit( 1 ); } // Read the first two lines of the input file at once fin >> day >> slash >> month >> slash >> year >> d1 >> d2 >> d3; fout << day << slash << month << slash << year << endl << d1 << " " << d2 << " " << d3 << endl; fin >> st >> x >> d >> ch; // Read the 3rd line, which is the first one of many similar lines while ( fin ) // As long as the file is not finished. This is the same as // // while ( !fin.eof() ) { fout << st << "\t" << x << "\t" << d << "\t" << ch << endl; fin >> st >> x >> d >> ch; } // close the file before terminating fin.close(); fout.close(); } void CopyFile3( char* infilename, char* outfilename ) { // This function reads an input file and copies it to an output file // The format of the input file is unknown char ch; ifstream fin; ofstream fout; fin.open( infilename ); // from this point, fin is the logical name of infilename if (!fin ) // if you can not open the file { cerr << " Could not open input file: " << infilename << endl; exit( 1 ); } fout.open( outfilename ); // from this point, fout is the logical name of outfilename if (!fout ) // if you can not open the file { cerr << " Could not open output file: " << outfilename << endl; fin.close(); // close the input file first. exit( 1 ); } // Attempt to read the first character from the input file. fin.get(ch); while ( fin ) // Check if the read attempt was successful // and continue to read as long as it is not // the end of the input file yet { fout.put(ch); // copy the character to the output file fin.get(ch); } // close the file before terminating fin.close(); fout.close(); } int main () { ShowFile( "payroll.txt" ); CopyFile1( "payroll.txt", "payroll.copy.txt"); CopyFile2( "payroll2.txt", "payroll2.copy.txt"); CopyFile3( "news.txt", "news.copy.txt"); return 0; } /* The output of running the program mark 12 19.3 p jhon 16 23.4 f Smith 23 15.3 p jack 12 34.5 f nancy 35 29.6 f */ // In addition, the files payroll.copy.txt, payroll2.copy.txt and news.copy.txt are created /* ======== payroll.txt ===== mark 12 19.3 p jhon 16 23.4 f Smith 23 15.3 p jack 12 34.5 f nancy 35 29.6 f ======== payroll.copy.txt - Created after you run the program ===== mark 12 19.3 p jhon 16 23.4 f Smith 23 15.3 p jack 12 34.5 f nancy 35 29.6 f ======== payroll2.txt ======== 18/4/1987 0.23 0.12 0.32 mark 12 19.3 p jhon 16 23.4 f Smith 23 15.3 p jack 12 34.5 f nancy 35 29.6 f ======== payroll2.copy.txt - Created after you run the program ======== 18/4/1987 0.23 0.12 0.32 mark 12 19.3 p jhon 16 23.4 f Smith 23 15.3 p jack 12 34.5 f nancy 35 29.6 f ======== news.txt ========= Cisco makes deal to push digital video over cable By Ben Heskett Staff Writer, CNET News.com November 11, 1999, 10:40 a.m. PT Cisco Systems made further moves into the cable networking equipment market today, acquiring start-up V-Bits for $128 million in stock. ======== news.copy.txt - Created after you run the program ========= Cisco makes deal to push digital video over cable By Ben Heskett Staff Writer, CNET News.com November 11, 1999, 10:40 a.m. PT Cisco Systems made further moves into the cable networking equipment market today, acquiring start-up V-Bits for $128 million in stock. */