关于fputc等之后的文件position
- C/C++ code
#include <stdio.h>int main(){ char buf; FILE *f = fopen("hello.txt", "ab+"); printf("%d\n", ftell(f)); fputc('c', f); printf("%d\n", ftell(f));}今天突然用到fputc()
发现和我之前想的不同.
hello.txt里面是
12345
输出:
0
5
fputc不是在当前position位置之后写吗???
我觉得结果应该是
0
1
hello.txt的内容应是c2345了
[解决办法]
和打开方式有关。
[解决办法]
关键是你的 a和后面的符号+
带有+时,表示以读写方式打开,文件指针在0
而写入数据时,是追加写入的
[解决办法]
关键是a