這裏我只是想輸入文件數據寫入我的類變量。類變量是公共的並且作爲通用變量。互聯網上有類似的話題,但花了數小時後,我仍然無法解決這個錯誤。如果我改寫((myfile >> map.at(i).x_p >> map.at(i).f)),那麼會有好的,但這並不可取。我的文件Book1.csv列示如下:C++讀取文件並寫入變量時出現未處理的異常 - getline錯誤?
9, 2.25
7, 3
5, 3
3, 3
1, 2.25
對於每一個對象,我還想寫2個變量,我寫我的代碼上的Visual Studio 2013快。 我很感激任何提示和建議。
這是我的代碼。
class node {
public:
string x_p;
string f; // force
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<node> map(5);
ifstream myfile;
myfile.open("Book1.csv");
if (myfile.is_open())
{
while ((getline(myfile, map.at(i).x_p, ','))) // close the loop when the file reading reaches its end
{
cout << "KK" << endl;
// getline(myfile, map.at(i).x_p, ',');
getline(myfile, map.at(i).f);
cout << map.at(i).x_p << ", " << map.at(i).f << endl;
++i;
}
myfile.close();
}
else cout << "Unable to open file";
cout << "end" << endl; //test
system("pause");
return 0;
}
你確切的錯誤是什麼? –
請提供MCVE。你當前的代碼甚至不會編譯。例如,'i'變量沒有被定義。 – Ari0nhh