從文件讀取我有一個看起來像一個文件:使用函數getline C++
Sister Act
Whoopi GoldBerg
Maggie Smith
Schwartz
Ardolino
Touch Stone Pictures
14
我無法讀取信息,並將其保存到一個對象。我沒有收到任何錯誤,但我無法使程序正確讀取信息。
我的問題是任何人都可以告訴我需要更改哪些內容才能讓我的程序正確讀取文件。
而且每行可以有除了整數多個單詞和空白行7
string title, starName1,
starName2, producer,
director, prodCo;
int numCopies;
ifstream videoFile("videoDat.txt");
if (videoFile.is_open()) {
getline(videoFile, title);
getline(videoFile, starName1);
getline(videoFile, starName2);
getline(videoFile, producer);
getline(videoFile, director);
getline(videoFile, prodCo);
//getline(videoFile, numCopies); //compiler error
while (videoFile >> title >> starName1 >> starName2 >> producer >> director >> prodCo >> numCopies) {
//be able to do stuff with variables individually
}
}
我想我需要做的是這樣的:
while (getline(videoFile, title) && getline(videoFile, starName1) && getline(videoFile, starName2)
&& getline(videoFile, producer) && getline(videoFile, director) && getline(videoFile, prodCo) && videoFile >> numCopies) {
//be able to do stuff with variables individually
}
請提供[MCVE(http://stackoverflow.com/help/mcve),這是很難推理編譯和正確性錯誤時,也有很多的片斷用戶定義類型。 – yeputons
我試圖編輯只留下給我沒有用戶定義類型的問題的代碼。 – Asuu