通过函数调用将从键盘输入的以#结束的字符串存入文件f1.txt,在将f1.txt内容复制到文件f2.txt
1个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
void saveTofile(FILE *fp)
{
char ch;
if((fp=fopen("f1.txt","wt"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
printf( "输入字符串:" );
while((ch=getchar())!='#')
fputc(ch,fp); /* 写文件 */
if( fclose(fp) )
{
printf( "Can not close the file!\n" );
exit(0);
}
}
void copyTofile(FILE *fp1,FILE *fp2)
{
if((fp1=fopen("f1.txt","rb"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
if((fp2=fopen("f2.txt","wb"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
while(!feof(fp1))
{
fputc(fgetc(fp1),fp2);
}
if( fclose(fp1) )
{
printf( "Can not close the file!\n" );
exit(0);
}
if( fclose(fp2) )
{
printf( "Can not close the file!\n" );
exit(0);
}
}
void main()
{
FILE *fp1,*fp2;
saveTofile(fp1);
copyTofile(fp1,fp2);
}
#include <stdlib.h>
void saveTofile(FILE *fp)
{
char ch;
if((fp=fopen("f1.txt","wt"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
printf( "输入字符串:" );
while((ch=getchar())!='#')
fputc(ch,fp); /* 写文件 */
if( fclose(fp) )
{
printf( "Can not close the file!\n" );
exit(0);
}
}
void copyTofile(FILE *fp1,FILE *fp2)
{
if((fp1=fopen("f1.txt","rb"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
if((fp2=fopen("f2.txt","wb"))==NULL) /* 打开文件 */
{
printf ( "File open error!\n" );
exit(0);
}
while(!feof(fp1))
{
fputc(fgetc(fp1),fp2);
}
if( fclose(fp1) )
{
printf( "Can not close the file!\n" );
exit(0);
}
if( fclose(fp2) )
{
printf( "Can not close the file!\n" );
exit(0);
}
}
void main()
{
FILE *fp1,*fp2;
saveTofile(fp1);
copyTofile(fp1,fp2);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询