前言

读取文件内容时,需要判断是否到文件末尾,此时用到eof函数

函数定义

Check whether eofbit is set
Returns true if theeofbiterror state flag is set for the stream.
This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream.
Note that the value returned by this function depends on the last operation performed on the stream (and not on the next.
Operations that attempt to read at the End-of-File fail, and thus both theeofbitand thefailbitend up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.
#include <fstream>
std::ios::eof
bool eof() const;

代码实例

// ios::eof example
#include <iostream> // std::cout
#include <fstream> // std::ifstream int main ()
{
std::ifstream is("example.txt"); // open file
char c;
while (is.get(c)) // loop getting single characters
std::cout << c; if (is.eof()) // check for EOF
std::cout << "[EoF reached]\n";
else
std::cout << "[error reading]\n";
is.close(); // close file
return ;
}

问题

何时说明到达末尾呢?

通过测试代码发现,读取文件内容之后再检测是否达到文件末尾。

测试代码

        while (!in_txt.eof())
{
if (in_txt.eof()) std::cout << "[EOF reached in while]" << std::endl;
getline(in_txt, inputName);
if (in_txt.eof()) std::cout << "[EOF reached after while]" << std::endl;
dir = dir1 + inputName;
dir += ".jpg";
std::cout << dir << std::endl;
}

测试输出

..//FDDB-originalPics//2002/08/07/big/img_1453.jpg
..//FDDB-originalPics//2002/08/02/big/img_760.jpg
[EOF reached after while]
..//FDDB-originalPics//.jpg
[EOF reached]
1: 1
02
..//FDDB-originalPics//2002/07/28/big/img_416.jpg

参考

1.cplusplus.com;

【c++基础】判断是否到文件末尾-eof函数的更多相关文章

  1. [C 语言基础] 如何调用不同文件中的函数

    很多时候需要将实现不同功能的函数或者与某个模块有关的函数写在一个文件里.这样有两个好处: 1. 方便以后调用:以后需要用到这个模块,或者这类函数,直接将相关文件复制过去,再稍微修改一下就能应用于不同场 ...

  2. 如何解决读取到文件末尾时碰到EOF导致的重复输出或者无用输出

    当读取到文件末尾时,会碰到EOF,如何解决呢?    方法一:我们可以通过(ch=fin.get())!=EOF来结束读取,这样就不会像eof()那样碰到EOF之后,还会再进行一次读取,导致输出一个无 ...

  3. python中判断readline读到文件末尾

    fp = open('somefile.txt') while True: line = fp.readline() if not line: #等价于if line == "": ...

  4. 文件末尾判断feof

    feof 作用:如果文件结束,则返回非0值,否则返回0 但要注意的是feof要读取到文件结束标志EOF后,才能判断文件是否结束. 所以使用while(!feof(pFile))会出现最后fread会返 ...

  5. hdu 1042 N! java大数及判断文件末尾

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submi ...

  6. 在TCP文件传输中如何判断java流的末尾

    感谢前辈们的解答:https://bbs.csdn.net/topics/280085530 问题描述: 服务端向客户端发送数据流,服务端发完了数据不关闭流. 我在客户端读流,我无法读到-1,所以无法 ...

  7. 零基础学Python--------第10章 文件及目录操作

    第10章 文件及目录操作 10.1 基本文件操作 在Python中,内置了文件(File)对象.在使用文件对象时,首先需要通过内置的open() 方法创建一个文件对象,然后通过对象提供的方法进行一些基 ...

  8. Go基础--终端操作和文件操作

    终端操作 操作终端相关的文件句柄常量os.Stdin:标准输入os.Stdout:标准输出os.Stderr:标准错误输出 关于终端操作的代码例子: package main import " ...

  9. 关于文件结束符EOF

    EOF 是 End Of File 的缩写. 在 C 语言中,它是在标准库中定义的一个宏. 人们经常误认为 EOF 是从文件中读取的一个字符(牢记).其实,EOF 不是一个字符,它被定义为是 int ...

随机推荐

  1. domContentload和onload

    defer和async <script src="script.js"></script> 没有 defer 或 async,浏览器会立即加载并执行指定的脚 ...

  2. w3c标准 dom对象 事件冒泡和事件捕获

    http://www.cnblogs.com/chengxs/p/6388779.html http://www.jb51.net/article/42492.htm W3C标准是什么? 1.表现(c ...

  3. C#中一个简单的匹配16进制颜色的正则测试

    using System; using System.Text.RegularExpressions; namespace Test { class Program { //匹配16进制颜色代码的正则 ...

  4. springboot集成mybatis及mybatis generator工具使用

    原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...

  5. hdoj2476 String painter

    题意:有一刷子,能将区间内涂成同一字母.给出src,dst串,问最少涂几次? 用dp[i][j]表示区间[i,j]内最少涂的次数.len=1,2时很明显.len=3时,dp[i][j]要么就在dp[i ...

  6. AndroidStudio使用偷懒插件Butterknife和GsonFormat

    1.Android ButterKnife Zelezny Android Studio上安装插件,如图: 配合ButterKnife实现注解,从此不用写findViewById,想着就爽啊.在Act ...

  7. President's Path CodeForces - 416E (最短路,计数)

    大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...

  8. 『Re』知识工程作业_主体识别

    作业要求 环境路径 类似于这样的,一共50篇文档, 均为中文文档,是法院判决书的合集. 程序 程序如下,我完全使用正则表达式来实现功能, import re import glob import co ...

  9. TitanX服务器登陆网关

  10. thinkphp条件查询

    1.这是我在做项目的时候编写的: $profit = M('shipping_types',' ','DB_PROFIT');//没有表前缀,在M函数的第二个参数就为空. //条件$field = a ...