使用C++从.txt文件中读取浮点数值,并将它们放入一个未知大小的二维数组中。

8
在C++中,我想读取一个有多列浮点数的文本文件,并将它们放入一个二维数组中。
第一行是第一列,依此类推。
数组的大小是未知的,它取决于可能会变化的行和列。
我尝试过使用"getline"、"inFile >>",但是所有修改都存在问题。
例如,有没有办法在值存在后删除不必要的行?
文件看起来像这样(+/-):
• 字符“\t”字符“\t”字符“\n” • 浮点数“\t”浮点数“\t”浮点数“\t”浮点数“\n” • 浮点数“\t”浮点数“\t”浮点数“\t”浮点数“\n” • 浮点数“\t”浮点数“\t”浮点数“\t”浮点数“\n”
谢谢。
到目前为止,我有这个:
int ReadFromFile(){

ifstream inFile;   
ofstream outFile;

int nLinActual = 0;
const int nCol = 9;
const int nLin = 10;

// open file for reading
inFile.open("values.txt");  
// checks if file opened
if(inFile.fail()) {
    cout << "error loading .txt file reading" << endl; 
    return 1;
}   
// open file for writing
outFile.open ("outArray.txt");  
// checks if file opened
if(outFile.fail()) {
    cout << "error loading .txt file for writing" << endl; 
    return 1;
}

// Doesn't read the first line
string dummyLine, dummyLine2, dummyLine3;
getline(inFile, dummyLine);

// Declares Array
float values[nLin][nCol];

//Fill Array with -1
for(int l=0; l<nLin; l++)
    for(int c=0; c<nCol; c++)
        values[l][c] = -1;

// reads file to end of *file*, not line 
while(!inFile.eof()) {

    for (int i=0; i<nCol; i++) {
        inFile >> values[i][nLinActual];
    }
    i=0;    
    ++nLinActual;
}

// Check what's happening
cout << endl;
for(int l=0; l<nLin; l++){
    for(int c=0; c<nCol; c++){
        cout << values[l][c] << "\t";
        outFile << values[l][c] << "\t";
    }
    cout << endl;
    outFile << endl;
}

inFile.close();
outFile.close();


return 0; 

}


1
最好贴一些代码并详细说明您遇到的“问题”。 - Jonathan Potter
文件长什么样?你能编辑你的帖子吗? - David G
我的代码现在能正常运行了,请参见以下内容。 - David G
2个回答

8
最简单的方法是使用向量:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

int main()
{
    std::fstream in("in.txt");
    std::string line;
    std::vector<std::vector<float>> v;
    int i = 0;

    while (std::getline(in, line))
    {
        float value;
        std::stringstream ss(line);

        v.push_back(std::vector<float>());

        while (ss >> value)
        {
            v[i].push_back(value);
        }
        ++i;
    }
}

更新:您说您需要使用原始的C数组完成。当然,这是可以完成的:
int main()
{
    std::ifstream in("in.txt");
    std::string line;

    float v[9][10];
    int i = 0, k = 0;

    while (std::getline(in, line))
    {
        float value;
        int k = 0;
        std::stringstream ss(line);

        while (ss >> value)
        {
            v[i][k] = value;
            ++k;
        }
        ++i;
    }
}

你不应该在内部循环中增加计数器吗? - Saksham
现在你错过了一个分号 :) - Saksham
能用数组实现吗?我已经发布了我的代码至今。 - JMG
但是如果我不知道每个数组的大小,我该如何做呢? - JMG

2

我认为这可能会对您有所帮助。使用一个类型为float的向量来存储数据,因为您不知道项目数量的计数。此代码假定您有每行用空格分隔的float数字。

fstream fs;
fs.open("abc.txt",ios::in);
vector<vector<float>> floatVec;
string strFloat;
float fNum;
int counter = 0;
while(getline(fs,strFloat))
{
    std::stringstream  linestream(strFloat);
    floatVec.push_back(std::vector<float>());
    while(linestream>>fNum)
        floatVec[counter].push_back(fNum);
    ++counter;
}

通常文本文件中的数字是由制表符或一些空格分隔的。使用数组可以实现吗? - JMG
@0x499602D2 实际上它对我编译通过了,但是抛出了一个异常。感谢您指出这个问题! - Saksham
@JMG虽然可能可以,但在不确定维度时最好不要使用数组。只需声明最大尺寸的数组,并在循环中使用两个索引来定义维度。 - Saksham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接