将文件读入数组C ++ [英] Reading file into array C++

查看:65
本文介绍了将文件读入数组C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试简单地读取文件"input.txt",排列成一个人[].txt文件包含3个数字:

I am trying to simply read a file "input.txt" into an array people[]. The txt file has 3 numbers:

10
20
30

对于人[0],我得到的是-9.25596e + 61,而不是10.这是我的代码:

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;
}

推荐答案

您的程序正确.工作正常.

Your program is correct. Working fine.

当前,它没有获取文件.所以,这对你来说是失败的.

Currently, it is not getting the file. So, it is failing for you.

按以下示例提供readFile()的完全限定路径:

Provide the fully qualified path to readFile() as below example:

Path like "C:\\Users\\source\\Temp\\x64\\Debug\\input.txt"

带有反斜杠或正斜杠的Windows支持文件路径:

Windows support file path with backward or forward slashes:

msdn.microsoft.com/zh-CN/library/aa365247(VS.85).aspx

msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

这篇关于将文件读入数组C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆