rewind 函数
如果一个文件具有读写属性,当我们写完文件,需要读的时候,此时会遇到文件 结尾现象。此时就需要 rewind。
ftell 函数
#include <stdio.h>
int main()
{
FILE* pf = fopen("qq.txt","w+");
fputs("abcdefg",pf);
int n = ftell(pf);
printf("%d", n);
return 0;
}
执行结果为:
由于返回值是当前读写位置到文件头部的字节数字所以打印结果为7,所以我们可以通过这个函数求得文件大小。
fseek函数
常见的起始位置有宏定义:
其中 SEEK_SET,SEEK_CUR 和 SEEK_END 和依次为 0,1 和 2.简言之:
代码演示:
#include <stdio.h>
int main()
{
FILE* fp = fopen("sql.txt", "w+");
fputs("123456789", fp);
fseek(fp, -5, SEEK_END);
int len = ftell(fp);
printf("len = %d\n", len);
return 0;
}
执行结果为:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
FILE* pf = fopen("qq.txt","w+");
fputs("abcdefg",pf);
int n = ftell(pf);
printf("%d\n", n);
rewind(pf);
n = ftell(pf);
printf("%d\n", n);
fseek(pf,0,SEEK_END);
n = ftell(pf);
printf("%d\n", n);
fseek(pf, 2, SEEK_SET);
n = ftell(pf);
fseek(pf, -4, SEEK_END);
n = ftell(pf);
printf("%d\n", n);
return 0;
}
执行结果为:
上面只是给出的一部里情况,读者可以根据函数说明验证参数的结果。