Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
/* 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);
}
Posted
Updated 24-Aug-15 9:01am
v2
Comments
PIEBALDconsult 24-Aug-15 15:04pm    
Do you get an error? If so, what is it?
Member 11932208 24-Aug-15 15:16pm    
program is running successfully until the file a.txt and b.txt has difference but when they are having same content the program goes to infinite!!!
i can't understand what is the problem? is there 'eof' problem? please check it on your editor.
thank you.
Patrice T 24-Aug-15 16:13pm    
Make short example text files and run the program on debugger, toy will see your program doing things, track variables changes, you should see where it don't do what you expect, it will narrow the research.
Afzaal Ahmad Zeeshan 24-Aug-15 15:05pm    
Program would throw an exception is most cases, what is the error message?
Member 11932208 24-Aug-15 15:16pm    
program is running successfully until the file a.txt and b.txt has difference but when they are having same content the program goes to infinite!!!
i can't understand what is the problem? is there 'eof' problem? please check it on your editor.
thank you.

1 solution

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
C
while(1)
{
   ch1 = getc(f1);
   ch2 = getc(f2);
   if (ch1 == EOF || ch2 == EOF) break; // exit the otherwise infinite loop
   if(ch1==ch2)
   // ...
 
Share this answer
 
v2
Comments
Member 11932208 24-Aug-15 16:00pm    
thank you very much!!!
Can i do this with .docx file?
CPallini 24-Aug-15 16:05pm    
Nothing is preventing you on that.
Dave Kreskowiak 24-Aug-15 16:11pm    
Yes, but it's meaningless to do a character compare. You should not comparing characters, but instead comparing bytes.

A .DOCX file is really just a .ZIP file with a bunch of other files and folders in it.
Member 11932208 24-Aug-15 16:15pm    
you mean if we create a .docx file through MS word and by c program have different meanings.
Dave Kreskowiak 24-Aug-15 16:24pm    
No, it means you have no idea what the content of a .DOCX file is compared to a text file and how to handle what you read from those files.

A file is really just a stream of bytes. What makes a text file a "text file" is how those bytes are interpreted when you read them. In your code, you're saying that those bytes must be treated as text, obeying encoding rules to correctly translate those bytes into text. Most encodings can fit a single character into a single byte, but some require multiple bytes to represent a single character.

A .DOCX file cannot be compared character by character because the bytes in the file do not following any encoding rules. For this kind of file you have to read each BYTE of the files and compare them, meaning not treating them as characters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900