1
我有些逗號在文件分隔符的數據,就像這樣:分配逗號分隔的數據,以矢量
116,88,0,44 66,45,11,33
等我知道了大小,我希望每一行都是它自己的向量中的對象。
這是我實現:
bool addObjects(string fileName) {
ifstream inputFile;
inputFile.open(fileName.c_str());
string fileLine;
stringstream stream1;
int element = 0;
if (!inputFile.is_open()) {
return false;
}
while(inputFile) {
getline(inputFile, fileLine); //Get the line from the file
MovingObj(fileLine); //Use an external class to parse the data by comma
stream1 << fileLine; //Assign the string to a stringstream
stream1 >> element; //Turn the string into an object for the vector
movingObjects.push_back(element); //Add the object to the vector
}
inputFile.close();
return true;
}
至今沒有運氣。我得到的
流1 < < fileLine
和聲明的push_back錯誤。 stream1告訴我沒有匹配運算符(應該是;我包括庫),後者告訴我moveObjects未聲明,似乎認爲它是一個函數,當它在頭中定義爲我的向量時。
任何人都可以在這裏提供任何幫助?非常感激!
'的std ::矢量 movingObjects();'是一個函數聲明,如果這是你寫的是什麼。要調用默認的構造函數,'std :: vector movingObjects {};'工作,但'std :: vector movingObjects;'也是如此。它不需要任何形式的括號/大括號。 –
chris
爲「矢量文件」搜索StackOverflow。迄今爲止,這些相關問題太多了。 –
另外,將'getline'放入循環條件,以便它不使用失敗的讀取。 – chris