【问题标题】:How to detect end-of-file when using freopen in C++?在 C++ 中使用 freopen 时如何检测文件结尾?
【发布时间】:2020-01-23 14:23:13
【问题描述】:

fopen 有 feof。但我正在使用

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);

这个相当于 feof 是什么?

【问题讨论】:

  • 我假设"w 是一个错字,应该是"w"
  • 为什么你认为它不一样?不过要小心feof。在尝试读取文件末尾之后确实如此。它不会告诉您您是否只是在文件末尾。
  • foef "for" freopen。一般情况下,feof 对应FILE*
  • 那就是feof
  • @Borgleader 谢谢,已修复。

标签: c++ file freopen


【解决方案1】:

和用fopen打开文件时一样:你用feof

例如,freopen("in.txt", "r", stdin); 之后,feof(stdin) 会告诉你是否打到“in.txt”的结尾。

关于feof 的常见警告适用:feof 不会告诉您是否在文件末尾现在 - 它会告诉您您是否尝试过最后一件事做失败了,因为你到了文件的末尾。见:Why is “while ( !feof (file) )” always wrong?

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2010-09-07
    • 2013-03-13
    相关资源
    最近更新 更多