返回fread功能 [英] Return of fread function

查看:75
本文介绍了返回fread功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文件需要阅读,我需要知道如何从字节中逐字节获取数据。



我尝试过:



i have some file to read and i need to know how can i get data from my file byte by byte.

What I have tried:

 for(j = 0; j < 140; j = j + 7)
{
   u16BytesRead = fread(&u8Buffer, 1, 7, fp); // what is the value of u16BytesRead  here ??

   }

推荐答案

如果要逐字节读取它,请更改7到1:

If you want to read it byte by byte, then change the 7 to a 1:
u16BytesRead = fread(&u8Buffer, 1, 1, fp);





如果你的意思是fread会返回什么?然后查看文档: fread - C ++ Reference [ ^ ]清楚地说:



If you mean "what does fread return?" then look at the documentation: fread - C++ Reference[^] which clearly says:

Quote:

返回值

返回成功读取的元素总数。

如果这个数字与count参数不同,要么发生读取错误,要么在读取时达到文件结尾。在这两种情况下,都会设置正确的指示符,可以分别使用ferror和feof进行检查。

如果size或count为零,则函数返回零,并且流状态和内容都指向由ptr保持不变。

size_t是无符号整数类型。

Return Value
The total number of elements successfully read is returned.
If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading. In both cases, the proper indicator is set, which can be checked with ferror and feof, respectively.
If either size or count is zero, the function returns zero and both the stream state and the content pointed by ptr remain unchanged.
size_t is an unsigned integral type.





但是只是在循环中调用fread不会帮助你 - 你每次打电话时都会覆盖你读过的数据。



也许如果你更详细地解释一下你想做什么,我们或许可以帮助你更多,但目前我们所能做的就是。



But just calling fread in a loop won't help you - you will just overwrite the data you read lats time each time you call it.

Perhaps if you explain what you are trying to do in better detail, we may be able to help you more, but at the moment that is all we can do.


引用:

我需要知道如何从字节中逐字节获取数据。

u16BytesRead的值是多少?

i need to know how can i get data from my file byte by byte.
what is the value of u16BytesRead here ??



当你基本上想要了解标准功能的工作原理,首先要阅读文档。

fread - C ++参考 [ ^ ]

fread - cppreference.com [ ^ ]

C库函数fread() [ ^ ]

文档可在互联网上获得,比我们的任何答案都要完整得多,而且比我们。



然后,如果您不确定,实验是了解什么是最好的方法。

使用调试器也允许你查看代码执行时发生了什么。


When you basically want to understand how a standard function works, reading the documentation is the first place to go.
fread - C++ Reference[^]
fread - cppreference.com[^]
C library function fread()[^]
Documentation is available on internet and is much more complete than any of our answers and faster than us.

Then if you are not sure, experimentation is the best way to know what is what.
Using the debugger also allow you to see what is going on in your code as it execute.


这篇关于返回fread功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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