C 相当于 c++ cin.peek()
C 编程中的 cin.peek() 相当于什么?我需要扫描文件中的“/r”和“/r/n”(这些是 DOS 文件的行尾标记),因此如果当前字符是“/r”,我需要“查看”下一个字符'
谢谢!
What is the equivalent of cin.peek() for C programming? I need to scan files for '/r' and '/r/n' (these are the end of line markers for DOS files) so I need to "peek" ahead to the next character if the current character is a '/r'
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有
ungetc()
,它允许您在查看字符时将其推回(就好像它们尚未读取一样)。http://www.zyba.com/reference/computing/c /stdio.h/ungetc.php
There is
ungetc()
, which allows you to push characters back (as if they were not already read), when you've peeked at them.http://www.zyba.com/reference/computing/c/stdio.h/ungetc.php
作为 read+
ungetc()
的替代方案,您可以逐字符读取文件,并跳过不必要的 '\r' 和/或 '\n' 或对它们执行任何其他特殊处理。一个简单的状态机可以提供帮助。As an alternative to read+
ungetc()
, you could read your file character by character and just skip unnecessary '\r' and/or '\n' or perform any other special handling of those. A trivial state machine can help.尝试:
或者:
如果您需要读取两个值(如“\n\r”),您可以使用短格式来存储它,并用它的数值进行测试
try:
or:
if you need to read two values(as "\n\r"), you can use a short for storing it, and the test with it's numeric value