fread()函数如何判断是否到文件末尾?
当用fread()函数将文件内容存到数组时,在靠近文件结尾的时候,会出现剩余的内容不足以填满数组或者已经到达结尾,这时要如何判断实际读出的数据大小?...
当用fread()函数将文件内容存到数组时,在靠近文件结尾的时候,会出现剩余的内容不足以填满数组或者已经到达结尾,这时要如何判断实际读出的数据大小?
展开
4个回答
展开全部
fread(从文件流读取数据)
相关函数 fopen,fwrite,fseek,fscanf
表头文件 #include<stdio.h>
定义函数 size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);
函数说明 fread()用来从文件流中读取数据。
参数stream为已打开的文件指针,
参数ptr 指向欲存放读取进来的数据空间,读取的字符数以参数size*nmemb来决定。
Fread()会返回 实际 读取到的nmemb数目,如果此值比参数nmemb 来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况。
返回值 返回实际读取到的nmemb数目。
附加说明
范例 #include<stdio.h>
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
int i;
stream = fopen(“/tmp/fwrite”,”r”);
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i<nmemb;i++)
printf(“name[%d]=%-20s:size[%d]=%d\n”,i,s[i].name,i,s[i].size);
}
执行 name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11
========================
feof(检查文件流是否读到了文件尾)
相关函数 fopen,fgetc,fgets,fread
表头文件 #include<stdio.h>
定义函数 int feof(FILE * stream);
函数说明 feof()用来侦测是否读取到了文件尾,尾数stream为fopen()所返回之文件指针。如果已到文件尾则返回非零值,其他情况返回0。
返回值 返回非零值代表已到达文件尾。
相关函数 fopen,fwrite,fseek,fscanf
表头文件 #include<stdio.h>
定义函数 size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);
函数说明 fread()用来从文件流中读取数据。
参数stream为已打开的文件指针,
参数ptr 指向欲存放读取进来的数据空间,读取的字符数以参数size*nmemb来决定。
Fread()会返回 实际 读取到的nmemb数目,如果此值比参数nmemb 来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况。
返回值 返回实际读取到的nmemb数目。
附加说明
范例 #include<stdio.h>
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
int i;
stream = fopen(“/tmp/fwrite”,”r”);
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i<nmemb;i++)
printf(“name[%d]=%-20s:size[%d]=%d\n”,i,s[i].name,i,s[i].size);
}
执行 name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11
========================
feof(检查文件流是否读到了文件尾)
相关函数 fopen,fgetc,fgets,fread
表头文件 #include<stdio.h>
定义函数 int feof(FILE * stream);
函数说明 feof()用来侦测是否读取到了文件尾,尾数stream为fopen()所返回之文件指针。如果已到文件尾则返回非零值,其他情况返回0。
返回值 返回非零值代表已到达文件尾。
展开全部
函数的原型如下
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
fread()返回的size_t类型数值就是实际大小啊,地球人都知道了!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
fread()函数的返回值就是实际读取的大小
对于用fopen函数打开的文件可以用feof来判断文件指针是否已到达文件尾
对于用fopen函数打开的文件可以用feof来判断文件指针是否已到达文件尾
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询