0
我正在讀取來自文件的輸入。我知道它的格式。所以我想讀一個文件的輸入並存儲它。我正在嘗試使用getline讀取最後一行,但程序只是掛起。這是我的輸入文件數據:由於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時,它不起作用......我做錯了什麼?謝謝。
原來我有第二個矩陣中的「OP」行之後。當我嘗試用這種方法來解決問題時,程序仍然被吊死。在操作線和第二個矩陣之間必須有一個換行符,但它沒有什麼區別。 –