> 唯美句子 > C语言文件操作问题,用fread读取

C语言文件操作问题,用fread读取

C语言文件操作问题,用fread读取一个文件,读完全部内容后文件指针是不是到了文件末尾?那么不关闭

可以用ferror和feof检测是否到达文件结尾

用fgetc会读到EOF

fread 读取 EOF

fread如果没有错误,正常读到函数末尾,那么返回的result比传入的number要小,如果最后正好相等,那么下次返回0。可以用feof(FILE*)来判断一下是否到末尾了,见下面代码

if(ferror(fp))

puts("I/O error when reading");

else if(feof(fp))

puts("End of file reached successfully");

fread()函数如何判断是否到文件末尾?

函数的原型如下

size_t fread(

void *buffer,

size_t size,

size_t count,

FILE *stream

);

fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged. If stream or buffer is a null pointer, fread invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

可见根据msdn,返回值表示具体读了多少出来,你根据返回值来做吧,呵呵。多看msdn

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

Remarks

The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.

This function locks out other threads. If you need a non-locking version, use _fread_nolock.

ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vccrt/html/9a3c1538-93dd-455e-ae48-77c1e23c53f0.htm

c语言中用fread和fwrite函数读数据时怎样判断文件结束?

使用feof()来判断,返回非0就是结束了。

if (feof(fp))

...... 此时就表明文件结束了,别再读了

C语言fread函数读取文件问题

fread是一个函数。从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功或读到文件末尾返回 0。

函数原型

size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;

参 数

buffer

用于接收数据的内存地址

size

要读的每个数据项的字节数,单位是字节

count

要读count个数据项,每个数据项size个字节.

stream

输入流

返回值

实际读取的元素个数。如果返回值与count不相同,则可能文件结尾或发生错误。从ferror和feof获取错误信息或检测是否到达文件结尾。

fread函数怎么读取文件的全部内容

fread是一个函数。从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功或读到文件末尾返回 0。 函数原型 size_t fread ( void *buffer, size_t size, size_t count, FILE *stream)...

fread 从文件读数据的时候,起始地址是文件中要读去数据的起始地址吗?

fread只是从文件当前指针读取指定大小的字节数到缓冲区,第一个参数是缓冲区的地址pBuff,第二个参数是你要读取数据类型数据的宽度SIZE,第三个是读取数据的个数N,第四个参数是文件流指针(fopen)打开创建。实际读取大小是SIZE*N个字节,当遇到文件尾时,读取的就是从当前指针到文件结尾的个数,该函数返回实际读取个数。

要说明的是,fread不支持结构化如链表的判断,相关逻辑结构需要编程者本人实现。

C语言fread为什么会读取失败呢?

fread是用来读取文件的函数,其形式为

size_t fread ( void *buffer, size_t size, size_t count, FILE *file) ;

功能为从文件指针file中,每次读取size长度数据,读取count次,并将结果存到buffer中。

其出错的可能原因包括:

1 文件没有打开,或打开失败。即文件指针非法。当文件不存在,或用户对文件没有读权限,目标文件被占用等情况下,打开文件会失败,这时file指针值为NULL。

2 文件打开时,并没有指定读属性。

3 文件IO错误。如在文件打开后,文件被其它程序强制删除,或取消可读属性等。

4 文件达到文件尾。如果文件已经读到文件尾,不存在size*count字节的可读数据,fread会出错。

5 传入的buffer非法。如buffer为空,或buffer拥有的空间不足以存储size*count字节的数据,导致越界访问。

以上即为常见的fread读操作出错的原因。实际出错的时候,需要结合现象及代码综合分析才能得到准确的结果。

fread读取字节小于实际文件里内容的字节时,fread是如何处理的?

MSDN解释如下:

fread returns the number of full items actually read,

which may be less than count if an error occurs or

if the end of the file is encountered before reaching count. Use the feof or

ferror function to distinguish a read error from an

end-of-file condition.

你的Item size为4, 试图读够你指定的4*1个字节,但是读到一个字节就遇到文件结束,没读够4个字节,所以返回0.

php fread()函数读取文本直到文件尾却无法结束

应该是!feof($contents=fread($handler,10)),这里返回的是字符串,不能用false去判断

C语言文件操作问题,用fread读取:等您坐沙发呢!

发表评论

表情
还能输入210个字