我想輸入一個帶有getline的分隔文本文件,但是當我調試它時,它顯示我在變量的開頭處有一個空字符。getline與分隔符存儲空字符
這隻發生在我的tID變量碰巧是每行中的第一個變量。當調試它示出了該作爲字符數組:
[0] = '' [1] = '2' [2] = '3' [3] = '4'
這裏是相關代碼:
ifstream inFile("books.txt");
if (!inFile){
cout << "File couldn't be opened." << endl;
return;
}
while(!inFile.eof()){
string tID, tTitle, tAuthor, tPublisher, tYear, tIsChecked;
getline(inFile,tID, ';');
getline(inFile,tTitle, ';');
getline(inFile,tAuthor, ';');
getline(inFile,tPublisher, ';');
getline(inFile,tYear, ';');
getline(inFile,tIsChecked, ';');
library.addBook(tID, tTitle, tAuthor, tPublisher, tYear, (tIsChecked == "0") ? false : true);
}
這裏有book.txt幾行:
123;C++ Primer Plus; Steven Prata; SAMS; 1998;0;
234;Data Structures and Algoriths; Adam Drozdek; Course Technlogy; 2005;0;
345;The Art of Public Speaking; Steven Lucas;McGraw-Hill;2009;0;
456;The Security Risk Assessment Handbook; Douglas J. Landall;Auerbach;2006;1;
您可以打印字符數組的ASCII值嗎?使用'printf(「%x」)'而不是'printf(「%c」)''。你可能在這裏懸掛'\ r's。 – 2011-04-11 22:57:06