您好我想打开读取和写入二进制文件(没有截断,没有附加)。我使用模式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.
更多推荐
二进制文件的打开方式
发布评论