第一小题
#include <stdio.h>
#include <stdlib.h>
//第一题,第一小题
int main()
{
FILE *fp ;
char ch,fname[10];
printf("文件名:");
gets(fname);
if ((fp=fopen(fname,"w"))==NULL)
{
printf("connot open\n");
exit(0);
}
while ((ch=getchar())!='#')
fputc(ch,fp);
fclose(fp);
return 0;
}
第二小题
#include <stdio.h>
#include <stdlib.h>
//第一题,第二小题,复制文件
int main()
{
FILE *fp1,*fp2 ;
char c;
if ((fp1=fopen("f1.dat", "r"))==NULL)
{
printf("connot open\n");
exit(0);
}
if ((fp2=fopen("f2.dat", "w"))==NULL)
{
printf("connot open\n");
exit(0);
}
c=fgetc(fp1);
while (c!=EOF)
{
fputc(c,fp2);
c=fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
return 0;
}
第三小题
#include <stdio.h>
#include <stdlib.h>
//第一题,第三小题,将文件file1.dat的内容输出到屏幕上并复制到文件file2.dat中
int main()
{
FILE *fp1,*fp2;
char ch;
if((fp1=fopen("f1.dat","r"))==NULL)
{
printf("Open file error\n");
exit(0);
}
if((fp2=fopen("f2.dat","w"))==NULL)
{
printf("Open file error\n");
exit(0);
}
while (!feof(fp1))
{
ch=fgetc(fp1);
putchar(ch);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2) ;
return 0;
}