0
我有以下文件:奇怪的函數getline行爲在C++中
BB
7.501106 5.324115
7.997006 8.287983
11.314904 11.768281
...
,我100%肯定該文件是好的,我甚至顯示在vim換行符與:set list
:
BB$
7.501106 5.324115$
7.997006 8.287983$
11.314904 11.768281$
...
但是當我打開並閱讀第一行時會發生一些奇怪的事情。我有以下代碼:
std::ifstream file(filename);
std::string line;
if (!file.is_open()) {
std::cerr << "parseConfig: Error opening config file: " << filename << std::endl;
exit(1);
}
getline(file, line);
std::cout << "line is: <" << line << ">" << std::endl;
if (line.compare("BB")) {
std::cerr << "parseConfig: Error in config file, first line is not BB" << std::endl;
exit(1);
}
現在我知道該文件正在打開正確,因爲我們一直到最後的錯誤。
打印出來是如下:
>ine is: <BB //What!!!?? Why did this happen?
parseConfig: Error in config file, first line is not BB
這令我奇怪,它彷彿有在文本文件中的回車。但我確信沒有。
任何想法?
是的。我如何將其轉換爲正確的格式? –
@Ben:'dos2unix BB' –