It is giving me an error when trying to run that I don't quite understand. |
What is the error?
Copy'n'paste the error text.
The error is misread file data.
Last edited on
std::istream::get reads a single character from a file, not an int. You are misreading the data in your file. That is your error.
http://www.cplusplus.com/reference/istream/istream/get/
A few fixes are needed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <fstream>
#include <vector>
int main()
{
std::vector<int> vec;
std::ifstream fin("vec.dat");
int vec_int { };
while (fin >> vec_int) { vec.push_back(vec_int); }
std::cout << "Read from the file:\n";
for (const auto& itr : vec) { std::cout << itr << ' '; } std::cout << '\n';
}
|
Read from the file:
26 19 45 3 82 123 22 51 30 87 74 |
Last edited on
It won't let me copy and paste. The error says:
Debug Assertion Failed!
Program: C:\Users ......
Line: 1455
Expression: Vector subscript out of range
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
Then gives me the option to Abort, Retry, or Ignore
Last edited on