问题
I am trying to simply read a file "input.txt" into an array people[]. The txt file has 3 numbers:
10
20
30
I am getting -9.25596e+61 instead of 10 for people[0]. Here is my code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Trip {
private:
double people[3];
public:
void readFile(string file);
};
void Trip::readFile(string file) {
ifstream input;
input.open(file);
input >> people[0] >> people[1] >> people[2];
cout << people[0];
input.close();
}
int main() {
Trip trip;
trip.readFile("input.txt");
return 0;
}
回答1:
Your program is correct. Working fine.
Currently, it is not getting the file. So, it is failing for you.
Provide the fully qualified path to readFile() as below example:
Path like "C:\\Users\\source\\Temp\\x64\\Debug\\input.txt"
Windows support file path with backward or forward slashes:
msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
来源:https://stackoverflow.com/questions/63930457/reading-file-into-array-c