前言

读取文件内容时,需要判断是否到文件末尾,此时用到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. TTFB

    TTFB    Time To First Byte 1.含义 最初的网络请求被发起”到“从服务器接收到第一个字节前”所花费的毫秒数 包含了 TCP连接时间,发送HTTP请求时间和获得响应消息第一个字 ...

  2. svg操纵方案 基于 D3 还是 angular?

    之前还是想简单了, 现在重新写这篇.把逻辑拆分粒度的辨析,放到外面去. 问题提出:svg控制方案 基于 D3 还是 angular 根据这个,html 4种展现样式:普通的html,svg,2D ca ...

  3. Lua面向对象 --- 单例

    GameManager.lua: --单例模式是利用一个全局表来实现的 GameManager = {} Manager = {__index = GameManager} function Game ...

  4. yii 第一步

    第一步:index.php // include Yii bootstrap filerequire_once(dirname(__FILE__).'/../../framework/yii.php' ...

  5. English trip -- VC(情景课) 6 C Is your class at 11:00? 你的课11点开始吗?

    Grammar focus 语法点 Is your class  at 11:00 ?  # 带be动词的一般疑问句 Yes, it is No, it isn't   相当于 is not  Pra ...

  6. codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

    题意:给一个数组,求其中任取2个元素,大的模小的结果最大值. 一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减.那么lower_bound(kx)的前一个就是最优 ...

  7. javaScript 的 map() reduce() foreach() filter()

    map(映射), reduce(规约), forEach(遍历), filter(过滤),它们都是高阶函数,都是以传入不同的函数来以不同的方式操作数组元.ie都不支持 一.map方法 *概述 map( ...

  8. android----HttpClient的get,post和图片上传服务器

    HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议.HttpCli ...

  9. protobuf3.5.1使用的简单例子

    前言 1. 什么是protobuf Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,是一种轻便高效的结构化数据存储格式,平台无关 ...

  10. Leetcode 92

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...