输入行结尾混合时的 std::getline 替代方案
我正在尝试从 std::istream
读取行,但输入可能包含 '\r'
和/或 '\n'
,所以 std::getline
没有用。
抱歉,但这似乎需要强调...
输入可能包含其中一个换行符类型或两者。
有没有标准的方法来做到这一点?目前我正在尝试
char c;
while (in >> c && '\n' != c && '\r' != c)
out .push_back (c);
......但这会跳过空白。噢! std::noskipws
-- 需要更多的摆弄,现在却很糟糕。
当然必须有更好的方法吗?!?
I'm trying to read in lines from a std::istream
but the input may contain '\r'
and/or '\n'
, so std::getline
is no use.
Sorry to shout but this seems to need emphasis...
The input may contain either newline type or both.
Is there a standard way to do this? At the moment I'm trying
char c;
while (in >> c && '\n' != c && '\r' != c)
out .push_back (c);
...but this skips over whitespace. D'oh! std::noskipws
-- more fiddling required and now it's misehaving.
Surely there must be a better way?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,这是一种方法。基本上我已经实现了 std::getline ,它接受谓词而不是字符。这让你完成了 2/3 的任务:
使用与此类似的函子:
现在,剩下的唯一事情就是确定你是否以
'\r'
结束...,如果如果你这样做了,那么如果下一个字符是'\n'
,只需使用它并忽略它。编辑:为了将这一切放入功能解决方案中,这里有一个示例:
我对原始示例做了一些细微的更改。
Pred p
参数现在是一个引用,以便谓词可以存储一些数据(特别是最后测试的char
)。同样,我将谓词operator()
设置为非 const,以便它可以存储该字符。在 main 中,我在
std::stringstream
中有一个字符串,它具有所有 3 个版本的换行符。我使用我的util::getline
,如果谓词对象表示最后一个char
是'\r'
,那么我peek()
前面并忽略1
字符(如果它恰好是'\n'
)。OK, here's one way to do it. Basically I've made an implementation of
std::getline
which accepts a predicate instead of a character. This gets you 2/3's of the way there:with a functor similar to this:
Now, the only thing left is to determine if you ended on a
'\r'
or not..., if you did, then if the next character is a'\n'
, just consume it and ignore it.EDIT: So to put this all into a functional solution, here's an example:
I've made a couple minor changes to my original example. The
Pred p
parameter is now a reference so that the predicate can store some data (specifically the lastchar
tested). And likewise I made the predicateoperator()
non-const so it can store that character.The in main, I have a string in a
std::stringstream
which has all 3 versions of line breaks. I use myutil::getline
, and if the predicate object says that the lastchar
was a'\r'
, then Ipeek()
ahead and ignore1
character if it happens to be'\n'
.读取一行的常用方法是使用
std::getline
。编辑:如果您的
std::getline
实现被破坏,您可以编写类似的内容,如下所示:我应该补充一点,从技术上讲,这可能不是
std 的问题::getline
被破坏,因为底层流实现被破坏了——由流将表示平台行结束的任何字符转换为换行符。然而,无论具体哪些部分被破坏,如果您的实现被破坏,这可能能够弥补它(话又说回来,如果您的实现被破坏得足够严重,则很难确定这是否会起作用)。The usual way to read a line is with
std::getline
.Edit: If your implementation of
std::getline
is broken, you could write something similar of your own, something like this:I should add that technically this probably isn't a matter of
std::getline
being broken, as of the underlying stream implementation being broken -- it's up to the stream to translate from whatever characters signify the end of a line for the platform, into a newline character. Regardless of exactly which parts are broken, however, if your implementation is broken, this may be able to make up for it (then again, if your implementation is broken badly enough, it's hard to be sure this will work either).