fork download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.  
  11. // Declare arrays of strings, ints, and chars to be inputted from
  12. // the file
  13. string strings[5] = {};
  14. int ints[5] = {}, numInts, numChars;
  15. char chars[5] = {};
  16.  
  17. // Declare variables to be used with our files
  18. ifstream fin;
  19. ofstream fout;
  20.  
  21. // Open the file in the input stream - note you must include
  22. // the file extension (".txt").
  23. fin.open("File IO - Example - In.txt");
  24.  
  25. // If we couldn't open the file, exit
  26. if(!fin.good())
  27. return 1;
  28.  
  29. // The actual data begins after a colon character, so ignore all data
  30. // until after the colon.
  31. fin.ignore(1000,':');
  32.  
  33. // Loop three times, inputting the strings from the file
  34. for(int i = 0; i < 3; i++)
  35. fin >> strings[i];
  36.  
  37. // Ignore the data until another colon
  38. fin.ignore(1000,':');
  39.  
  40. // Input the number of ints to input, and loop that many times. Note
  41. // that if this is greater than five, this will try to go off the end
  42. // of the array and create an error.
  43. fin >> numInts;
  44. for(int i = 0; i < numInts; i++)
  45. fin >> ints[i];
  46.  
  47. // Ignore the data until another colon
  48. fin.ignore(1000,':');
  49.  
  50. // Input characters until the file says it cannot input any more data.
  51. // Again, if this happens more than five times you will get an error,
  52. // as 5 is the size of the chars array.
  53. for(numChars = 0; fin.good(); numChars++)
  54. fin >> chars[numChars];
  55.  
  56. // Decrement numChars, as it will have incremented one too many times.
  57. // When fin.good() returns false, numChars will have already been incremented.
  58. numChars--;
  59.  
  60. // Close the input file, as we are done with it
  61. fin.close();
  62.  
  63.  
  64. // Output the data to the console so we can see the input worked
  65. // correctly
  66. for(int i = 0; i < 3; i++)
  67. cout << strings[i] << " ";
  68. cout << endl;
  69. for(int i = 0; i < numInts; i++)
  70. cout << ints[i] << " ";
  71. cout << endl;
  72. for(int i = 0; i < numChars; i++)
  73. cout << chars[i] << " ";
  74. cout << endl;
  75.  
  76.  
  77. // Open the output file - note this will create the file it it does not
  78. // already exist. If it already exists, it will overwrite it.
  79. fout.open("File IO - Example - Out.txt");
  80.  
  81. // Output the same results to the file.
  82. // The process is exactly the same, except using "fout" instead of "cout."
  83. for(int i = 0; i < 3; i++)
  84. fout << strings[i] << " ";
  85. fout << endl;
  86. for(int i = 0; i < numInts; i++)
  87. fout << ints[i] << " ";
  88. fout << endl;
  89. for(int i = 0; i < numChars; i++)
  90. fout << chars[i] << " ";
  91. fout << endl;
  92.  
  93. // Close the output file, as we are done with it.
  94. fout.close();
  95.  
  96. cout << endl;
  97. system("pause");
  98.  
  99. return 0;
  100. }
  101.  
Success #stdin #stdout #stderr 0s 3464KB
stdin
Standard input is empty
stdout
     
0 0 0 0 0 
     

stderr
sh: 1: pause: not found