0

ファイルから入力を読み取っています。私はその形式を知っています。そのため、ファイルから入力を読み取って保存したいと思います。getline を使用して最後の行を読み込もうとしていますが、プログラムが単にハングします。これは私の入力ファイルデータです:

6
1 2 2 -4 45 32
123 4234 -234 34534 54 2344
1 2 2 3 4 -234
2 3 -4 -4 4 234
1 11 123 1234 -12334563 2342
2 -234 -23 23 4322 2342
op 2

入力ファイルの最初の値は、正方行列の行/列の数を示します。次に、正方行列自体があります。最後に、操作コードがあります。

これは私のコードです:

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

using namespace std;

int main()
{
    int i,j,matlim;
    int num;
    string matrixlimit;
    string inputfile;
    string operation;
    ifstream data;
    vector< vector<int> > mat1storage,mat2storage;

    cout<<"Please enter an input file name: ";
    cin>>inputfile;    

    data.open(inputfile.c_str());
    if(data.is_open())
    {
    data >> matlim;
    while(!data.eof())
    {
        for(i = 0;i<matlim;i++)
        {
            vector<int> mat1row;
            for (j = 0;j<matlim;j++)
            {
                data >> num;
                mat1row.push_back(num);
            }
            mat1storage.push_back(mat1row);
        }

        getline(data,operation);
        cout << operation;

    }
 }
 data.close();
}

ファイルから最後の行を読み取るためにループで単純な「データ >> 操作」を実行すると、プログラムは問題なく動作します。しかし、getline を使用しようとすると機能しません...何が間違っていますか? ありがとうございました。

4

1 に答える 1

0

Getline はデータを読み取り、区切り文字が見つかるまで文字列に格納します。

区切り文字は\n

op 2テキストファイルの後に空白の改行を挿入すると、機能するはずです

于 2012-11-18T03:21:41.823 に答える