scanf从文件中读入,printf写入到文件
【摘要】 重定向方式读写文件
#include <stdio.h>
#define LOCAL
int main()
{ #ifdef LOCAL freopen("input.txt","r",stdin); //使得scanf从文件input.txt读入 //r只读,如果文件不存在,出错 freopen("output.txt","w",stdout); //使得print...
重定向方式读写文件
#include <stdio.h>
#define LOCAL
int main()
{ #ifdef LOCAL freopen("input.txt","r",stdin); //使得scanf从文件input.txt读入 //r只读,如果文件不存在,出错 freopen("output.txt","w",stdout); //使得printf写入文件output.txt //w只写,如果文件不存在,建立新文件 #endif //只有定义了符号LOCAL,才编译2条freopen语句。 int j; scanf("%d",&j); for(int i=0;i<5;i++) printf("%d\n",i); printf("%d\n",j); return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
非重定向方式读写文件
int main()
{ FILE *fin,*fout; fin=fopen("data.in.txt","r"); fout=fopen("data.out.txt","w"); int j; fscanf(fin,"%d",&j); for(int i=0;i<5;i++) fprintf(fout,"%d\n",i); fprintf(fout,"%d\n",j); fclose(fin); fclose(fout); return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。
原文链接:chenhx.blog.csdn.net/article/details/48350403
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)