Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本文件中的矩阵存储在C++;_C++_Arrays_Integer_Text Files - Fatal编程技术网

C++ 将文本文件中的矩阵存储在C++;

C++ 将文本文件中的矩阵存储在C++;,c++,arrays,integer,text-files,C++,Arrays,Integer,Text Files,你好,非常感谢 =2&&lineno让您开始: 用于将字符串(每行)作为流进行操作 用于将字符串转换为双精度 不要检查eof(),它不会按照您的想法执行。相反,您可以检查getline()返回的值 一个最小的工作示例可以如下所示(可以进行优化,但我希望它易于阅读): intmain() { std::ifstream文件(“matrix.txt”); 如果(!文件) 抛出std::runtime_错误(“打开文件时出错”); 双矩阵[4][3]; std::字符串行; 无符号i=0; wh

你好,非常感谢

<我是C++的新手,对不起,如果我的起点是垃圾…我需要打开并读取一个文本文件,以便应用一些数值程序,但我发现很难找出这一步

接下来,您可以看到文本数据。您如何阅读本文,将这些值/数字存储在维度为[3,4]的矩阵中

readata.txt:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1    0.00000e+00    0.00000e+00
    2    1.00000e+00    0.00000e+00
    3    0.00000e+00    -1.00000e+00
    4    2.00000e+00    -2.00000e+00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

我被这个起始代码卡住了

string line;
ifstream myfile ("readata.txt");
if (myfile.is_open())
{
    while (! myfile.eof() )
{
for (int lineno = 0; getline (myfile,line) ; lineno++)
{
        if (lineno >= 2 && lineno <= 5)
        {
            cout << line << endl;
        }
        else
        {};
    }
    myfile.close();
}
else cout << "Unable to open file";
字符串行;
ifstream myfile(“readata.txt”);
如果(myfile.is_open())
{
而(!myfile.eof())
{
for(int lineno=0;getline(myfile,line);lineno++)
{
如果(lineno>=2&&lineno让您开始:

  • 用于将字符串(每行)作为流进行操作
  • 用于将
    字符串
    转换为
    双精度
  • 不要检查
    eof()
    ,它不会按照您的想法执行。相反,您可以检查
    getline()
    返回的值

一个最小的工作示例可以如下所示(可以进行优化,但我希望它易于阅读):

intmain()
{
std::ifstream文件(“matrix.txt”);
如果(!文件)
抛出std::runtime_错误(“打开文件时出错”);
双矩阵[4][3];
std::字符串行;
无符号i=0;
while(std::getline(文件,行))
{
std::stringstream-ss;
ss>x>>y>>z;
双xd=std::stod(x);
双yd=std::stod(y);
双zd=std::stod(z);

std::不能使用a来处理您的行。不要在(!myfile.eof())
时使用
,请学习缩进代码。编写不可读的代码只会给您带来坏习惯,这些坏习惯会在(很快)的将来再次困扰您。(你是对的Joachim。)你能给我一些代码说明吗?我已经尝试了一些方法,但无法将其付诸实施…学习如何使用
std::stringstream
。创建一个包含整数和两个双精度的类。创建该类型的
std::vector
,将stringstream中的值解析到该类的实例中并
push_back()
将该实例导入向量。
int main()
{
    std::ifstream file("matrix.txt");

    if(!file)
        throw std::runtime_error("Error opening file");

    double matrix[4][3];
    std::string line;
    unsigned i = 0;
    while(std::getline(file, line))
    {
        std::stringstream ss;
        ss << line;
        std::string x, y, z;
        ss >> x >> y >> z;

        double xd = std::stod(x);
        double yd = std::stod(y);
        double zd = std::stod(z);

        std::cout << xd << " " << yd << " " << zd << '\n';
        matrix[i][0] = xd;
        matrix[i][1] = yd;
        matrix[i][2] = zd;
        i++;
    }
    return 0;
}