下面一段c语言代码实现从一个文本文件(distance.txt)读数据,再写入另一个文本文件(distance_out.txt)。很简单的文件操作程序,却一直没成功!
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
float miles;
FILE *inp, *outp;
inp = fopen("f: distance.txt","r");
outp = fopen("f: distance_out.txt", "w");
if((inp == NULL))
{
printf("cannot open file\n");
exit(0);
}
fscanf(inp, "%f",&miles);
fprintf(outp,"The distance in file is %.2f\n", miles);
printf("The distance in file is %3.3f", miles);
fclose(inp);
fclose(outp);
return(0);
}
最后经过几番查找,终于找到答案:
inp = fopen("f: distance.txt","r");
outp = fopen("f: distance_out.txt", "w&q