Reading file into array C++

时光毁灭记忆、已成空白 提交于 2021-01-29 18:18:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!