Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- //Function Declarations
- // ----------------------------------------------------------
- // Open specified input file.
- // ----------------------------------------------------------
- void OpenInputFile(ifstream & inF, char inFileName[]);
- void OpenInputFile(ifstream & inF, char inFileName[])
- {
- inF.open(inFileName); //Opens input file.
- if(inF.fail()) //Checks if the file was opened successfully.
- {
- return; //Terminates program if file is not found.
- }
- }
- // ----------------------------------------------------------
- // Read a value from input file; set success to FALSE if
- // value can not be read.
- // ----------------------------------------------------------
- void ReadValue (istream & inF, int & num, bool & success);
- void ReadValue (istream & inF, int & num, bool & success)
- {
- inF >> num; //Reads a value within the input file.
- success = !inF.fail(); //Declares that success means a value has been read.
- }
- // ---------------------------------------------------------------
- // Write a value to output stream, up to eight(8) values per line.
- // Separate values by ONE SPACE.
- // ---------------------------------------------------------------
- void WriteValue (ostream & outF, int num);
- void WriteValue (ostream & outF, int num)
- {
- outF << num << " "; //Print the read value and add a space between each value.
- }
- //Main function.
- int main()
- {
- // ----------------------------------------------------------------------
- // Declare variables.
- // ----------------------------------------------------------------------
- // FORMAT: <type> <variable>; // Declaration comment.
- int numIn;
- bool success;
- char infilename[256]; //Name of the string value that holds the input file name.
- char outfilename[256]; //Name of the string value that holds the output file name.
- ifstream inF; //Declared input file.
- ofstream outF; //Declared output file.
- //-| ----------------------------------------------------------------------
- //-| 1. Read names of input and output files.
- //-| ----------------------------------------------------------------------
- cout << "Enter Name of Input File: "; //Prompts for input filename.
- cin >> infilename; //Enter input filename.
- cout << "Enter Name of Output File: "; //Prompts for output filename.
- cin >> outfilename; //Enter output filename.
- cout << endl;
- //-| ----------------------------------------------------------------------
- //-| 2. Open input file. If this fails, terminate the program.
- //-| ----------------------------------------------------------------------
- OpenInputFile(inF, infilename); //Opens input file.
- //-| ----------------------------------------------------------------------
- //-| 3. Open output file.
- //-| ----------------------------------------------------------------------
- outF.open(outfilename); //Opens output file.
- //-| ----------------------------------------------------------------------
- //-| 4. Read contents of input file.
- //-| ----------------------------------------------------------------------
- ReadValue (inF, numIn, success); //Read the first value.
- //-| ----------------------------------------------------------------------
- //-| 5. Write all values of input files into output file.
- //-| ----------------------------------------------------------------------
- while(success) //Do this when a value has been successfully read.
- {
- WriteValue (outF, numIn); //Write the read value.
- ReadValue (inF, numIn, success); //Read the next value.
- }
- //-| ----------------------------------------------------------------------
- //-| 6. Close input files.
- //-| ----------------------------------------------------------------------
- inF.close(); //Close the input file.
- system("pause");
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement