结论:fseek在 fopen 带有‘a‘模式的文件指针偏移不起作用。
int main(int argc, char *argv[])
{
FILE * fp = NULL;
char buf[10] = {0};
int size = 0;
int i;
for (i = 0; i < 10; ++i) {
buf[i] = i + ‘a‘;
}
fp=fopen("test_file","ab+");
if(!fp)
{
return 0;
}
size = 10;
fwrite(buf, sizeof(char), size, fp);
fseek(fp, 0, SEEK_SET);
fwrite(buf, sizeof(char), size, fp);
fclose(fp);
fp = NULL;
return 0;
return 0;
}
可以通过hexdump test_file -C
获取结果。
$ hexdump aa -C
00000000 61 62 63 64 65 66 67 68 69 6a 61 62 63 64 65 66 |abcdefghijabcdef|
00000010 67 68 69 6a |ghij|
00000014
原文:https://www.cnblogs.com/schips/p/13680809.html