文件复制问题,做半天都是错的,球大婶解答
程序虽然很挫,但是凑活能看,运行后 结果数组filename1【10】 中会莫名其秒的出现个不认识的字符然后我 只能球大神帮助了 谢谢
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
FILE *fp1;
char ch,filename[10];
char choose;
scanf("%c",&choose);
if(choose=='r')
{
scanf("%s",filename);
if((fp=fopen(filename,"rt"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
ch=getchar();
while(!(feof(fp)))
{
ch=fgetc(fp);
putchar(ch);
}
putchar(10);
fclose(fp);
}
if(choose=='w')
{
scanf("%s",filename);
if((fp=fopen(filename,"wt"))==NULL)
{
printf("cannot open this file\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!='\n')
{
fputc(ch,fp);
putchar(ch);
ch=getchar();
}
putchar(10);
fclose(fp);
}
if(choose=='c')
{
char a[50]={0};
int cnt=1;
int i;
char filename1[10];
char filename2[10];
scanf("%s",filename1);
scanf("%s",filename2);
if((fp=fopen(filename1,"r"))==NULL)
{
printf("Cannot open this file");
exit(0);
}
a[0]=fgetc(fp);
while(!feof(fp))
{
a[cnt++]=fgetc(fp);
}
if((fp1=fopen(filename2,"w"))==NULL)
{
printf("Cannot open this file");
exit(0);
}
for(i=0;i<cnt;i++)
{
ch=a[i];
fputc(ch,fp1);
}
printf("%s\n",a);
fclose(fp);
fclose(fp1);
}
}