文件尾和文件结束标识

aston00 2016-04-18 05:29:36
fseek(fp,0,SEEK_END);
offset=ftell(fp);
printf("offset=%d\n",offset);

if(feof(fp)){
printf("feof=%d\n",feof(fp));
printf("the end of file \n");
我将文件位置指针移动至文件尾,但是feof却没有检测到文件结束标志,也就是if(!feof(fp))为假 ,请问是什么原因。
文件尾和文件结束标识是对等的么
...全文
712 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
LubinLew 2016-04-19
  • 打赏
  • 举报
回复
feof是测试stream是否设置end-of-file标志,而ftell/seek函数不会设置end-of-file标志符。 你看下面的例子,如果你个文件有5个字节长,那么你从读完5个字节后,feof仍然返回0, 说明文件流没有被end-of-file标志,如果你再多读取一个1个字符,feof就返回非0值了。 那么道理就明白了吧,fseek设置文件指针到文件末尾,末尾也是在文件范围内,只有出 了这个范围才是end-of-file

#include <stdio.h>

int main(void)
{
	FILE* fp = NULL;
	long offset = 0;
	int ret = 0;
	char buf[4096];
	
	fp = fopen("words", "rb");
	if (NULL == fp) {
		return -1;
	}
	
	fseek(fp, 0, SEEK_END);
	offset = ftell(fp);
    printf("offset=%ld\n", offset);
    
    //reset
	fseek(fp, 0, SEEK_SET);
	fread(buf, 1, offset, fp);

	ret = feof(fp);
	printf("feof return %d\n", ret);
	if (ret) {
		printf("the end of file \n");
	}
	fclose(fp);
	return 0;
}
fornetuse123 2016-04-19
  • 打赏
  • 举报
回复
参考楼上的文章, 1、你可能没有在程序开头 #include <stdio.h> #include <stdlib.h> 2、feof函数在文件结尾会返回一个非0值,这个值跟ftell函数在文件尾读到的offset不知是否一样?你可以采用楼上的count计数方法获得offset
赵4老师 2016-04-19
  • 打赏
  • 举报
回复
feof Tests for end-of-file on a stream. int feof( FILE *stream ); Function Required Header Compatibility feof <stdio.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return. Parameter stream Pointer to FILE structure Remarks The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it. Example /* FEOF.C: This program uses feof to indicate when * it reaches the end of the file FEOF.C. It also * checks for errors with ferror. */ #include <stdio.h> #include <stdlib.h> void main( void ) { int count, total = 0; char buffer[100]; FILE *stream; if( (stream = fopen( "feof.c", "r" )) == NULL ) exit( 1 ); /* Cycle until end of file reached: */ while( !feof( stream ) ) { /* Attempt to read in 10 bytes: */ count = fread( buffer, sizeof( char ), 100, stream ); if( ferror( stream ) ) { perror( "Read error" ); break; } /* Total up actual bytes read */ total += count; } printf( "Number of bytes read = %d\n", total ); fclose( stream ); } Output Number of bytes read = 745 Error Handling Routines | Stream I/O Routines See Also clearerr, _eof, ferror, perror

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧