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