c ++ getline读取整个文件 [英] c++ getline reads entire file
问题描述
我使用std :: getline()从一个文本文件中逐行读取。但是,第一次调用getline是在整个文件中读取!我也试过明确指定分隔符为'\\\
'。
std :: ifstream serialIn;
...
serialIn.open(argv [3]);
...
std :: string tmpStr;
std :: getline(serialIn,tmpStr,'\\\
');
//文件中的所有570行都在tmpStr!
...
std :: string serialLine;
std :: getline(serialIn,serialLine);
// serialLine ==here
我使用Visual Studio 2008。文件有570行(我在Notepad ++ fwiw中查看它)。
编辑:我使用Notepad ++转换行结束在我的输入文本文件到Windows行尾。使用c ++代码在每行的末尾使用\\\
编写文件。为什么getline()需要Windows行结尾(\r\\\
)?这与字符宽度或Microsoft实现有关?
只是猜测,但你的文件可能有Unix行结尾您在Windows上运行?
I'm using std::getline() to read from a text file, line by line. However, the first call to getline is reading in the entire file! I've also tried specifying the delimeter as '\n' explicitly. Any ideas why this might be happening?
My code:
std::ifstream serialIn;
...
serialIn.open(argv[3]);
...
std::string tmpStr;
std::getline(serialIn, tmpStr, '\n');
// All 570 lines in the file is in tmpStr!
...
std::string serialLine;
std::getline(serialIn, serialLine);
// serialLine == "" here
I am using Visual Studio 2008. The text file has 570 lines (I'm viewing it in Notepad++ fwiw).
Edit: I worked around this problem by using Notepad++ to convert the line endings in my input text file to "Windows" line endings. The file was written with '\n' at the end of each line, using c++ code. Why would getline() require the Windows line endings (\r\n)?? Does this have to do with character width, or Microsoft implementation?
Just guessing, but could your file have Unix line-endings and you're running on Windows?
这篇关于c ++ getline读取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!