Advertisement
Guest User

Copying Input Into Output.

a guest
Sep 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. //Function Declarations
  6.  
  7. // ----------------------------------------------------------
  8. // Open specified input file.
  9. // ----------------------------------------------------------
  10. void OpenInputFile(ifstream & inF, char inFileName[]);
  11.  
  12. void OpenInputFile(ifstream & inF, char inFileName[])
  13. {
  14.     inF.open(inFileName);               //Opens input file.
  15.  
  16.     if(inF.fail())                  //Checks if the file was opened successfully.
  17.     {
  18.         return;                 //Terminates program if file is not found.
  19.     }
  20. }
  21.  
  22. // ----------------------------------------------------------
  23. // Read a value from input file; set success to FALSE if
  24. //      value can not be read.
  25. // ----------------------------------------------------------
  26. void ReadValue (istream & inF, int & num, bool & success);
  27.  
  28. void ReadValue (istream & inF, int & num, bool & success)
  29. {
  30.     inF >> num;                 //Reads a value within the input file.
  31.     success = !inF.fail();              //Declares that success means a value has been read.
  32. }
  33.  
  34. // ---------------------------------------------------------------
  35. // Write a value to output stream, up to eight(8) values per line.
  36. //    Separate values by ONE SPACE.
  37. // ---------------------------------------------------------------
  38. void WriteValue (ostream & outF, int num);
  39.  
  40. void WriteValue (ostream & outF, int num)  
  41. {
  42.     outF << num << " ";             //Print the read value and add a space between each value.
  43. }
  44.  
  45. //Main function.
  46. int main()
  47. {
  48.     // ----------------------------------------------------------------------
  49.     //  Declare variables.
  50.     // ----------------------------------------------------------------------
  51.     // FORMAT: <type> <variable>;           // Declaration comment.
  52.  
  53.     int numIn;
  54.     bool success;
  55.     char infilename[256];               //Name of the string value that holds the input file name.
  56.     char outfilename[256];              //Name of the string value that holds the output file name.
  57.     ifstream inF;                   //Declared input file.
  58.     ofstream outF;                  //Declared output file.
  59.  
  60.     //-| ----------------------------------------------------------------------
  61.     //-| 1. Read names of input and output files.
  62.     //-| ----------------------------------------------------------------------
  63.  
  64.     cout << "Enter Name of Input File: ";       //Prompts for input filename.
  65.     cin >> infilename;              //Enter input filename.
  66.  
  67.     cout << "Enter Name of Output File: ";      //Prompts for output filename.
  68.     cin >> outfilename;             //Enter output filename.
  69.  
  70.     cout << endl;
  71.  
  72.     //-| ----------------------------------------------------------------------
  73.     //-| 2. Open input file. If this fails, terminate the program.
  74.     //-| ----------------------------------------------------------------------
  75.    
  76.     OpenInputFile(inF, infilename);         //Opens input file.
  77.  
  78.     //-| ----------------------------------------------------------------------
  79.     //-| 3. Open output file.
  80.     //-| ----------------------------------------------------------------------
  81.  
  82.     outF.open(outfilename);             //Opens output file.
  83.  
  84.     //-| ----------------------------------------------------------------------
  85.     //-| 4. Read contents of input file.
  86.     //-| ----------------------------------------------------------------------
  87.  
  88.     ReadValue (inF, numIn, success);        //Read the first value.
  89.  
  90.     //-| ----------------------------------------------------------------------
  91.     //-| 5. Write all values of input files into output file.
  92.     //-| ----------------------------------------------------------------------
  93.  
  94.     while(success)                  //Do this when a value has been successfully read.
  95.     {
  96.         WriteValue (outF, numIn);       //Write the read value.
  97.         ReadValue (inF, numIn, success);    //Read the next value.
  98.     }
  99.  
  100.     //-| ----------------------------------------------------------------------
  101.     //-| 6. Close input files.
  102.     //-| ----------------------------------------------------------------------
  103.  
  104.     inF.close();                    //Close the input file.
  105.  
  106.     system("pause");
  107.     return 0;
  108.  
  109. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement