fork download
  1. // Attached: HW_6b
  2. // ===========================================================
  3. // File: HW_6b.cpp
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CS 1A
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. // ==== main ===================================================
  16. // This program reads numbers from the data.txt file and
  17. // writes them to the results.txt file.
  18. //
  19. // Input:
  20. // data.txt - A text file containing integer values.
  21. //
  22. // Output:
  23. // The numbers are written to results.txt, and a message
  24. // is displayed on the screen.
  25. // ===========================================================
  26.  
  27. int main()
  28. {
  29. ifstream inFile;
  30. ofstream outFile;
  31. int number;
  32.  
  33. // open the input and output files
  34. inFile.open("data.txt");
  35. outFile.open("results.txt");
  36.  
  37. // check if the input file opened successfully
  38. if (!inFile)
  39. {
  40. cout << "Error opening file!" << endl;
  41. return 0;
  42. }
  43.  
  44. // write each number from data.txt into results.txt
  45. while (inFile >> number)
  46. {
  47. outFile << number << endl;
  48. }
  49.  
  50. inFile.close();
  51. outFile.close();
  52.  
  53. cout << "The data has been written to the file." << endl;
  54. cout << "Press any key to continue . . .";
  55. cin.get();
  56.  
  57. return 0;
  58.  
  59. } // end of main()
  60.  
  61. // ===========================================================
Success #stdin #stdout 0.01s 5324KB
stdin
5
12
8
20
3
stdout
Error opening file!