今天写一个小功能需要读取文件,在判断文件结尾时使用了以下语句:

     while(infile.eof() && infile.good())
{
infile.read((char*)&data, );
encodeData = data ^ CODE;
outfile.write((char*)&encodeData, );
}

结果最后一个字符会读取两次,后来换了种方式,事先读取文件长度,再循环,就能争取读取文件:

    for(int i=; i<file_len; ++i)
{
infile.read((char*)&data, );
encodeData = data ^ CODE;
outfile.write((char*)&encodeData, );
}

最后了解到,是因为read函数需要读取失败时才将eofbit置位,所以置位时,循环还会执行一次。修改代码就没有问题了:

    while(infile.read((char*)&data, ) && !infile.eof() && infile.good())
{
encodeData = data ^ CODE;
outfile.write((char*)&encodeData, );
}

读取文件时,使用file.eof()判断结尾注意事项的更多相关文章

  1. 【转】C#读取文件时的共享方式

    string sFileName = @"C:\Exchange.dat";System.IO.StreamReader file = new System.IO.StreamRe ...

  2. python在读取文件时出现 'gbk' codec can't decode byte 0x89 in position 68: illegal multibyte sequence

    python在读取文件时出现“UnicodeDecodeError:'gbk' codec can't decode byte 0x89 in position 68: illegal multiby ...

  3. Python读取文件时出现UnicodeDecodeError 'gbk' codec can't decode byte 0x80 in position x

    Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据出现错误: UnicodeDecode ...

  4. Python读取文件时出现UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position xx: 解决方案

    Python在读取文件时 with open('article.txt') as f: # 打开新的文本 text_new = f.read() # 读取文本数据 出现错误: UnicodeDecod ...

  5. Java读取文件时第一行出现乱码“?”问号

    我们在使用Java在读取文件(txt.dat等)时,如果文件不是utf-8格式的话,读取结果会出现,中文字符变乱码的情况,所以一般在读取时转为UTF-8格式读取. 但这时会出现一种情况,第一次读取第一 ...

  6. nodejs读取文件时相对路径的正确写法(使用fs模块)

    在开发nodejs中,我们往往需要读取文件或者写入文件,最常用的模块就是fs核心模块.一个最简单的写入文件的代码如下(暂时不考虑回调函数): fs.readFile("./test.txt& ...

  7. Java读取文件时中文路径处理

    读取文件路径时可能存在以下情况: 1.空格,如果出现空格会转变成“%20” 2.中文路径,如果出现中文路径会变成URI编码“%e5%bc%80%e5%8f%91%e5%b7%a5%e7%a8%8b” ...

  8. node.js在读取文件时中文乱码问题

    断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...

  9. Java_I/O输入输出_实现读取文件时出现一个表示读取进度的进度条。可以使用java.swing包提供的输入流类ProgressMonitorInputStream

    import java.io.*; import javax.swing.*; public class Student { public static void main(String[] temp ...

随机推荐

  1. php字符串 统计个数

    方法一 $arr=str_split($str); $arr=array_count_values($arr); /* * 方法二 * */ $arr = str_split($str); $a2 = ...

  2. leetcode-algorithms-28 Implement strStr()

    leetcode-algorithms-28 Implement strStr() mplement strStr(). Return the index of the first occurrenc ...

  3. leetcode-algorithms-5 Longest Palindromic Substring

    leetcode-algorithms-5 Longest Palindromic Substring Given a string s, find the longest palindromic s ...

  4. atoi函数原型

    一.atoi()函数的功能: 1.定义: 将字符串转换成整型数,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')结束转化,并将结果返回(返回转换后的整型数). ...

  5. 兼容IE8的video写法

    <video width="100%" height="515px" controls preload> <source src=" ...

  6. C# 3.0 / C# 3.5 扩展方法

    概述 扩展方法是一种特殊的静态方法,可以像扩展类型上的实例方法一样进行调用,能向现有类型“添加”方法,而无须创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法的定义实现: public s ...

  7. 部署java项目到服务器

    1.首先判断服务器是什么系统 linux,windows 2.如果是linux使用SSH进行链接 3.如果是windows使用远程桌面进行链接 1.windows+R->mstsc进行远程桌面的 ...

  8. id: cannot find name for user ID xxx处理办法

    一.现像 root用户登录显示正常但以普通用户登录时报类似如下错误: id: cannot find name for user ID 500id: cannot find name for grou ...

  9. linux查看主机最后启动时间

    1.使用who查看最后重启时间 who -b 2.使用last查看近几次重启时间 last reboot 第一条即为最后一次重启日期 last其实是查询用户的登录记录,reboot是一个伪用户:也就是 ...

  10. RWCString 定义 memeroy leak

    代码截取片段: testDefs.hh class testDefs { public: static const RWCString testStr; }; testDefs.cc const RW ...