fprintf关于写入文件中顺序

来源:互联网 发布:linux tmpwatch 编辑:程序博客网 时间:2024/06/06 09:36

首先,我们确认的是fprintf(fp,“%d”,a);

其中a为整形变量 我们来看一下下面的程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(){
int a=1,n=1;
while(n<20){
FILE *fp=fopen("123.txt","a+");
fprintf(fp,"%d\n",a);
a++;
n++;
fclose(fp);
}
}

输出结果为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

说明其输出结果是从末尾加入的。

然后我们将“a+”换为其他方式:

1.w+  结果为  19显然程序运行完毕后前面所有输入123.txt中的数据全部被清空

2.a  结果与a+相同

3 w与w+相同

原创粉丝点击