fork download
  1. // Attached: HW_6a
  2. // ===========================================================
  3. // File: HW_6a.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. // displays them on the screen.
  18. //
  19. // Input:
  20. // data.txt - A text file containing integer values.
  21. //
  22. // Output:
  23. // The numbers in the file are displayed on the screen.
  24. // ===========================================================
  25.  
  26. int main()
  27. {
  28. ifstream inFile;
  29. int number;
  30.  
  31. // open the file for reading
  32. inFile.open("data.txt");
  33.  
  34. // check if the file opened successfully
  35. if (!inFile)
  36. {
  37. cout << "Error opening file!" << endl;
  38. return 0;
  39. }
  40.  
  41. cout << "Here are the numbers in the file:" << endl;
  42.  
  43. // read and display each number in the file
  44. while (inFile >> number)
  45. {
  46. cout << number << endl;
  47. }
  48.  
  49. inFile.close();
  50.  
  51. cout << "Press any key to continue . . .";
  52. cin.get();
  53.  
  54. return 0;
  55.  
  56. } // end of main()
  57.  
  58. // ===========================================================
Success #stdin #stdout 0s 5320KB
stdin
12
7
25
3
19
stdout
Error opening file!