我有以下文件:
BB
7.501106 5.324115
7.997006 8.287983
11.314904 11.768281
...
我100%确定该文件没问题,我甚至在:set list
的vim中显示了换行符:
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
这让我觉得很奇怪,好像文本文件中有回车符。但我很确定没有。
有什么想法吗?
答案 0 :(得分:3)
看起来该文件处于DOS模式。检查vim底部是否显示[dos]
,或选中file yourfile.txt
。
另一种检查方法是通过cat -A
来管理文件或程序输出(如果您的猫没有cat -v
,则为-A
)。回车将显示为^M
。
要转换为UNIX格式,请在vim中执行:set ff=unix
,然后保存文件。或者如果有的话,使用dos2unix
命令行工具。