我写了一个c程序来比较两个文本文件并将其写入一个新的文本文件,但第三个文本文件没有打开! [英] i wrote a c program to compare two text file and write it to a new text file but the third text file is not open!

查看:57
本文介绍了我写了一个c程序来比较两个文本文件并将其写入一个新的文本文件,但第三个文本文件没有打开!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / *  这是我的代码。请检查并回复我(电子邮件)我在哪里创建的错误! * /  

#include < stdio.h >
#include < conio .h >
#include < io.h > ;


void main()
{

FILE * f1,* f2,* f3;
char ch1,ch2;

clrscr();
f1 = fopen( a.txt r);
if (f1 == NULL)
{
printf( 无法打开第一个文件\ n);
}
else
{
printf( 打开一个文件。\ n);
}


f2 = fopen( b.txt r);
if (f2 == NULL)
{
printf( 无法打开第二个文件\ n);
getch();
退出( 1 );
}
else
{
printf( b文件已打开。);
}

printf( 我们准备好了!) ;

f3 = fopen( c.txt w);
if (f3 == NULL)
{
printf( 文件未创建!);
退出( 1 );
}
else
printf( \ n文件三创建了\ n);
while (f1!= NULL&& f2!= NULL)
{
ch1 = getc(f1);
ch2 = getc(f2);
if (ch1 == ch2)
{
putc(ch2,f3);
继续;
}
else
{
printf( \ n char1 =%c and char =%c,ch1,ch2);
getch();
退出( 1 );
}
} fflush(stdin);
// ch2 ='\ 0';
// putc(ch2,f3);
fclose(f3);
printf( \ n工作完成!);
getch();
fclose(f1);
fclose(f2);
}

解决方案

Quote:

while(f1!= NULL&& f2!= NULL)



读取文件时这不是一个好的停止条件。

当到达文件结尾时,其FILE指针不会变为NULL。

文件结束条件由 getc()返回值表示 EOF (参见getc)。

更改

引用:

while(f1!= NULL&& f2!= NULL)

{

ch1 = getc(f1);

ch2 = getc(f2);

if(ch1 == ch2)

to

  while  1 
{
ch1 = getc(f1);
ch2 = getc(f2);
if (ch1 == EOF || ch2 == EOF) break ; // 退出无限循环
if (ch1 == ch2)
// ...


/* Here is my code. please check it and reply me(e-mail)  where is/are the mistake i have created! */

#include<stdio.h>
#include<conio.h>
#include<io.h>


void main()
{

   FILE *f1, *f2, *f3;
   char ch1,ch2;

   clrscr();
   f1 = fopen("a.txt","r");
   if(f1 == NULL)
   {
	printf("Cannot open first file\n");
   }
   else
   {
	printf("a file is opened.\n");
   }


   f2 = fopen("b.txt","r");
   if(f2==NULL)
      {
	printf("Cannot open second file\n");
	getch();
	exit(1);
      }
   else
   {
	printf("b file is opened.");
   }

   printf("Here we ready!");

   f3 = fopen("c.txt","w");
   if(f3==NULL)
   {
	printf("file is not created!");
	exit(1);
   }
   else
	printf("\nfile three is created\n");
   while(f1!=NULL && f2!=NULL)
   {
      ch1 = getc(f1);
      ch2 = getc(f2);
      if(ch1==ch2)
      {
	putc(ch2,f3);
	continue;
      }
      else
      {
	 printf("\n char1 = %c and char = %c",ch1,ch2);
	 getch();
	 exit(1);
      }
   }          fflush(stdin);
  // ch2='\0';
   //putc(ch2,f3);
   fclose(f3);
   printf("\n Work completed!");
   getch();
   fclose(f1);
   fclose(f2);
}

解决方案

Quote:

while(f1!=NULL && f2!=NULL)


This is NOT a good stop condition while reading files.
When the end-of-file is reached, its FILE pointer doesn't become NULL.
The end-of-file condition is signaled by the getc() return value being EOF (see "getc" ).
Change from

Quote:

while(f1!=NULL && f2!=NULL)
{
ch1 = getc(f1);
ch2 = getc(f2);
if(ch1==ch2)

to

while(1)
{
   ch1 = getc(f1);
   ch2 = getc(f2);
   if (ch1 == EOF || ch2 == EOF) break; // exit the otherwise infinite loop
   if(ch1==ch2)
   // ...


这篇关于我写了一个c程序来比较两个文本文件并将其写入一个新的文本文件,但第三个文本文件没有打开!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆