0

我搜索我的问题,但没有找到答案。我尝试将文件中的双精度值表读取到 C++ 中的二维双精度数组中,但我无法让它工作。

该文件充满了我不需要的其他垃圾,并且该表由“BEGIN TABLE”和“END TABLE”括起来。该表连续有 5 个双精度数,带有空格分隔符和未知的行数。所以文件看起来像这样

junk
.
.
.
BEGIN TABLE
0.12145 0.23234 2.32423 1.32422 0.12345
1.34534 1.23423 5.21323 3.12313 1.22231
.
.
.
2.32422 3.23423 1.12345 4.34532 2.23423
END TABLE

所以首先我浏览文件,搜索表的开头和结尾并为我的数组分配内存:

char sBuffer[100];
double** darrTable;
int iRes = -1;

    iRes = fopen_s(&pFile, strFile, "rb");
    if (iRes==0)
    {
        int ilines = 0;
        bool beof = false;
        bool bfound = false;
        //get number of lines for array allocation
        while(!beof)
        {

            fgets(sBuffer,100,pFile);                   
            if(strstr(sBuffer,"END TABLE"))
            {
                bfound = false;
                beof = true;
            }
            if(bfound) ilines++;
            if(strstr(sBuffer,"BEGIN TABLE"))bfound = true; 
        }
        darrTable = new double*[ilines+1];
        for(int i = 0; i < (ilines+1); ++i) darrTable [i] = new double[5];
    }

在另一个代码块中,我再次遍历这些行并想读出字符串,但它不起作用

int ilines = 0;
bool beof = false;
bool bfound = false;
while(!beof)
            {
                fgets(sBuffer,100,pFile);                   
                if(strstr(sBuffer,"END TABLE"))
                {
                    bfound = false;
                    beof = true;
                }
                if(bfound)
                {   
                    sscanf_s(sBuffer,"%d %d %d %d %d",&darrTable [ilines][0],&darrTable [ilines][1],&darrTable [ilines][2],&darrTable [ilines][3],&darrTable [ilines][4]);

                    ilines++;
                }
                if(strstr(sBuffer,"BEGIN TABLE"))bfound = true; 

            }

它编译并运行没有错误,但我得到的只是一个充满 0.000000000 的数组,用于 darrTable。sscanf_s 返回 1 女巫建议找到 1 个值,但所有值都是 0。

我使用 VisualStudio 2005 SP0。

对不起我的英语,并提前感谢您的帮助。

4

1 回答 1

0

使用 C++ 风格

#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

std::vector<std::vector<double>> ParseFile(const std::string& filename)
{
    std::vector<std::vector<double>> data;

    std::ifstream file(filename.c_str());
    std::string line;

    while (std::getline(file, line))
    {
        if (line == "BEGIN TABLE")
        {
            while (std::getline(file, line))
            {
                if (line == "END TABLE")
                {
                    break;
                }
                else
                {
                    data.push_back(std::vector<double>());
                    std::istringstream ss(line);
                    double temp;
                    while (ss >> temp)
                    {
                        data.back().push_back(temp);
                    }

                    if (data.back().size() != 5)
                    {
                        // missing or extra data
                    }
                }
            }
            break; // END TABLE missing
        }
    }

    if (data.empty())
    {
        // BEGIN TABLE missing or there was no data lines
    }

    return data;
}
于 2013-10-01T12:59:25.427 回答