fseek函数的用法(用于设定指针位置)
fseek函数用来设置文件指针stream的位置,原型为:
int fseek(FILE *stream, long offset, int fromwhere);
下面先对fseek有一个初步的认识,以后具体用到时,如果不清楚,可以查阅相关(网络)资料.
#include<iostream>
using namespace std;
int main()
{
int offset;
int fromWhere; // 0表示文件开头
FILE *fp = fopen("myData.txt", "w");
fprintf(fp, "123456");
cout << ftell(fp) << endl;
offset = 0;
fromWhere = 0;
fseek(fp, offset, fromWhere);
cout << ftell(fp) << endl;
offset = 1;
fromWhere = 0;
fseek(fp, offset, fromWhere);
cout << ftell(fp) << endl;
fclose(fp);
cout << "*********************" << endl;
int a[10];
memset(a, 0, sizeof(a));
fp = fopen("yourData", "wb");
fwrite(a, sizeof(a), 1, fp);
cout << ftell(fp) << endl;
offset = 5;
fromWhere = 0;
fseek(fp, offset, fromWhere);
cout << ftell(fp) << endl;
fclose(fp);
return 0;
}
结果为:
6
0
1
*********************
40
5
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
