请教高手:一个C语言问题!

来源:百度知道 编辑:UC知道 时间:2024/06/06 06:31:19
题目:编程把文本文件阿a.c复制到文本文件b.c中,要求仅复制x1.dat中非空格字符。
帮我看看我的程序哪里错了啊,多谢!
#include<stdio.h>
#include<stdlib.h>
void main(){
FILE *fp,*in,*out;
char ch,filename[10],infile[10],outfile[10];
printf("input filename:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{printf("cannot open file\n");
exit (0);}
printf("Input string to first file end with #.");
ch=getchar();
while(ch!='#')
{fputc(ch,fp);
ch=getchar();}
fclose(fp);
printf("input copy soure filename:");
scanf("%s",infile);
if((in=fopen(infile,"r"))==NULL)
{printf("cannot open file\n");}
printf("input copy to filename:");
scanf("%s",outfile);
if((out=fopen(outfile,"w"))==NULL)
{printf("cannot open file\n");}

while(!

共有两个错误:1.第一个getchar()接受的字符不正确。在使用scanf输入数据时,界面中输入了一个回车符。第一个getchar()则接受了这个回车符。
2.最后部分流程不太清楚。
正确的如下:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp,*in,*out;
char ch,filename[10],infile[10],outfile[10];
char temp = ' ';
printf("input filename:");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit (0);
}
printf("Input string to first file end with #.");

getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);

printf("input copy soure filename:");
scanf("%s",infile);
if((in=fopen(infile,"r"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
printf("input co