c++ getline 读不到东西_c ++ getline读取整个文件

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值