如何解决将文件读入数组C ++
我试图将文件“ input.txt”简单地读入数组people []。 txt文件包含3个数字:
10
20
30
对于人[0],我得到的是-9.25596e + 61,而不是10。这是我的代码:
#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;
}
解决方法
您的程序正确。工作正常。
当前,它没有获取文件。所以,这对你来说是失败的。
按以下示例提供readFile()的完全限定路径:
Path like "C:\\Users\\source\\Temp\\x64\\Debug\\input.txt"
带有反斜杠或正斜杠的Windows支持文件路径:
msdn.microsoft.com/zh-cn/library/aa365247(VS.85).aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。