C语言实现文件写入
实现了文件的基本操作
操作方法
- 01
首先打开VC++6.0
- 02
选择文件,新建
- 03
选择C++ source file 新建一个空白文档
- 04
先声明头文件 #include <stdio.h>
- 05
写上主函数 void main() { }
- 06
主要代码 FILE *infile,*outfile,*otherfile; char input; char inputs[10]; int i=0; infile = fopen("d:\\infile.txt","r+");//用fopen函数打开文件 outfile = fopen("d:\\outfile.txt","a+");//用fopen函数打开文件 if ( !infile ) printf("open infile failed....\n"); if ( !outfile) printf("open outfile failed...\n"); printf("*********************************************\n"); printf("** This program is to show file operation! **\n"); printf("** The input file is: **\n"); printf("** d:\\infile.txt **\n"); printf("** The contents in this file is: **\n"); printf("\n"); for(;;) { input = fgetc(infile);//死循环读出文件内容 printf("%c",input); putc(input,outfile);//写入内容 i++; if(input == '\n' || input == EOF) break; } fclose(infile); fclose(outfile); scanf("%d",i);
- 07
运行结果