二进制文件的打开方式 [英] Opening mode of Binary files

查看:224
本文介绍了二进制文件的打开方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想打开读取和写入二进制文件(没有截断,没有附加)。我使用模式R + B。我没有得到任何编译错误,但是当读什么我写我得到垃圾值。任何想法。

Hi I want to open a binary file for both reading and writing(without truncating, not appending). I use mode "r+b". I get no compiling errors but when read what I have written I get garbage values. Any idea.

但是,如果我打开文件写(模式为世行)..写入in..close,然后打开该文件进行读取(模式为RB),a和读取它,然后它工作正常。

But if I open the file for writing(mode as "wb")..write into in..close it and then open the file for reading(mode as "rb") aand read from it ,then it works fine.

推荐答案

当你打开读/写模式的文件,如R +,W +,你需要确保你的文件指针指向一个有效的位置。 fseek的()这里是你的朋友。

When you open a file in read/write modes such as "r+", "w+", you need to make sure your file pointer points to a valid position. fseek() is your friend here.

此外,作为一个很好的做法,记得要经常从函数检查返回值。在这种情况下,也许你的FREAD()都失败了没有你注意到它。

Also, as a good practice, remember to always check returned values from functions. In this case, maybe your fread() have failed without you noticing it.

这篇关于二进制文件的打开方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆