如何在多维数组中组织文本文件中的数据并从中乘以列

How do you organize data from a text file in multidimensional arrays and multiply columns from it?

本文关键字:数据 文件 数组 文本      更新时间:2023-10-16

抱歉,我对 c++ 有点陌生,但我需要将 txt 文件中的数据组织成数组(或向量,如果这样更容易(,它需要有 12 列和 10000 行。我需要能够将这些列相乘,但我无法将数据放入行中。数据按选项卡解析,并且已采用 12x10000 格式。如何仅使用 c++ 执行此操作?

我已经尝试过上网查找,除了阅读文本之外,我什么都没有。我还有 225 行代码,这是我尝试这样做的所有尝试。它基本上归结为这些线。我有一个解析器,但它除了按选项卡划分数据外什么都不做,而不是识别它。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    float array[12][10000]; // creates array to hold names
    long loop=0; //short for loop for input
    float line; //this will contain the data read from the file
    ifstream myfile ("data.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
            getline (myfile,line); //get one line from the file
            array[loop] = line;
            cout << array[loop] << endl; //and output it
            loop++;
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");
    return 0;
}

我希望结果是组织成数组或向量的数据(我不知道如何使用向量(,我可以在其中乘以列,但它只是伴随着我无法正确将代码放入列中的错误。

这里有一个简单的解决方案,适用于制表符或空格的分隔符。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
constexpr size_t rows_len = 10000;
constexpr size_t cols_len = 12;
int main ()
{
    float array[rows_len][cols_len]{}; // value initialization to ensure unfilled cells at 0
    ifstream myfile("data.txt");
    if (!myfile.is_open()) {
        cout << "Unable to open file" << endl;
        return 1;
    }
    string line;
    for (size_t row = 0; row < rows_len && myfile; row++) {
        getline(myfile, line);
        const char* s = line.c_str();
        for (size_t col = 0; col < cols_len; col++) {
            char* p = nullptr;
            array[row][col] = strtof(s, &p);
            s = p;
        }
    }
    // use array ...
    return 0;
}

strtof()的第二个参数允许知道下一个单元格的开头在哪里。如果单元格不是数字,则所有剩余的array行都设置为 0。

如果您确定将遵循输入格式,您可以简单地让 iostream 模块解码浮点值:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    float array[12][10000]; // creates array to hold names
    long loop=0; //short for loop for input
    float line; //this will contain the data read from the file
    ifstream myfile ("data.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        for(int line=0; line <1000; line++) //read 1000 lines
        {
            for (int col=0; col<12; col++)  // 12 values per line
            {
                myfile >> arr[col][line];
                if (!myfile)
                {
                    cout << "Read error line " << line << " col " << col << "n";
                    myfile.close();
                    return 1;
                }
            }
        }
        myfile.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");
    return 0;
}

请注意,此代码不使用皱眉的 while (! myfile.eof(( (,而是在读取后立即进行测试