2024年4月29日发(作者:)
用来测试当前读写位置是否到达文件末尾的函数
要测试当前读写位置是否到达文件末尾,可以使用以下函数:
1. feof函数:该函数在文件结束时返回非零值
```c
#include
#include
int mai
FILE *file = fopen("", "r");
if (file == NULL)
printf("Error opening file.n");
exit(1);
}
fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾
if (feof(file))
printf("File pointer is at the end of file.n");
} else
printf("File pointer is not at the end of file.n");
}
fclose(file);
return 0;
```
2. ftell函数:该函数返回文件指针的当前位置,通过比较返回值
和文件大小来判断是否到达文件末尾
```c
#include
#include
int mai
FILE *file = fopen("", "r");
if (file == NULL)
printf("Error opening file.n");
exit(1);
}
fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾
long filesize = ftell(file); // 获取文件大小
fseek(file, 0, SEEK_CUR); // 将文件指针移动到当前位置
long currentpos = ftell(file); // 获取当前位置
if (currentpos == filesize)
printf("File pointer is at the end of file.n");
} else
printf("File pointer is not at the end of file.n");
}
fclose(file);
return 0;
```
注意,以上示例假设文件以只读方式打开,并且文件名为
。函数返回非零值表示已到达文件末尾,返回0表示未到达
文件末尾。请根据实际情况进行修改。
发布评论