0

問題を検索しましたが、答えが見つかりません。ファイルから double 値のテーブルを C++ の double の 2d 配列に読み込もうとしましたが、うまくいきません。

ファイルは必要のない他のジャンクでいっぱいで、テーブルは「BEGIN TABLE」と「END TABLE」で括弧で囲まれています。テーブルには、スペース区切り文字と行数が不明な 5 つの double が連続しています。したがって、ファイルは次のようになります

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; 

            }

エラーなしでコンパイルおよび実行されますが、得られるのは、darrTable の 0.000000000 でいっぱいの配列だけです。sscanf_s returns 1 witch は、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 に答える