Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to read every character from a two files: PISMENA.TXT and PISMENA2.txt and then compare them (one from the PISMENA and one from PISMENA2). But I have a problem in this part:

while(((fscanf(fr_pismena,"%c",&character_pismena))!=EOF)&&((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF)){
     printf("\nZ prveho suboru:%c"
            "\nZ druheho suboru:%c",character_pismena, character_pismena2);
     if(character_pismena!=character_pismena2)
       count_of_differences++;
     else
     ;
   }

because when the PISMENA.TXT has got a (for example) a 13 characters and PISMENA2.TXT has got a 14 characters then end of the program is after 13th character.
When both files has got same number of characters then everything is ok...

Sorry for my english, I hope, you understand... If you need a full code, I can post it.
Thanks
Posted
Updated 15-Nov-14 8:06am
v2

Create two more while blocks after the while checking each file singularly for EOF. The one which is not EOF will be scanned char by char and automatically be counted as difference.

C++
// ... (your code at the moment)

// When you are here you will have at most ONE file which is not EOF,
// so only ONE of the loops will be executed
while ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){
    count_of_differences++;
}

while ((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){
    count_of_differences++;
}
 
Share this answer
 
Or initialize count_of_differences to be the difference of the two lengths rather than zero.

If this is not a homework assignment, you might prefer to look into Levenshtein Distance.
http://en.wikipedia.org/wiki/Levenshtein_distance[^]
 
Share this answer
 
den2k88 Thank you very much, PIEBALDconsult too ! :)

den2k88 I had a one next problem with short-circuit evaluation (&&), that's why i wrote it differently, but your way helped me very much (I used it), so thx very much !

Now full code: //anything in czech language :D
Objective-C
#include <stdio.h>
#include <conio.h>
#include <time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{
    FILE *fr_pismena,*fr_pismena2;
    char character_pismena;
    char character_pismena2;
    int count_of_differences=0;

    openfile_1:
    if((fr_pismena=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'PISMENA.TXT' for read. "
             "Press 'Enter' to try again or 'q' to close the program.");

      repair_open_1:
      switch(getchar()){
      case 'q':return 0;break;
      case '\012':goto openfile_1;
      default    :printf("Incorrect character, try again..");goto repair_open_1;
      }
    }
    else
      printf("\nThe file PISMENA.TXT is successfully opened.\n\n");


    openfile_2:
    if((fr_pismena2=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.2\\PISMENA2.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'PISMENA2.TXT' for read. "
             "Press 'Enter' to try again or 'q' to close the program.");

      repair_open_2:
      switch(getch()){
      case 'q':return 0;break;
      case '\012':goto openfile_2;
      default    :printf("Incorrect character, try again..");goto repair_open_2;
      }
    }
    else
      printf("\nThe PISMENA2.TXT is successfully opened.\n\n");

    while(1){
      if(fscanf(fr_pismena,"%c",&character_pismena)!=EOF){
        if(fscanf(fr_pismena2,"%c",&character_pismena2)!=EOF){
          printf("\nZ prveho suboru:%c"
                 "\nZ druheho suboru:%c",character_pismena, character_pismena2);
          if(character_pismena!=character_pismena2)
            count_of_differences++;
          else
          ;
        }
        else{
          ungetc(character_pismena,fr_pismena);
          break;
        }
      }
      else{
        break;
      }
    }

    while((fscanf(fr_pismena2,"%c",&character_pismena2))!=EOF){
      count_of_differences++;
    }
    while ((fscanf(fr_pismena,"%c",&character_pismena))!=EOF){
      count_of_differences++;
    }

    if(count_of_differences==0)
      printf("\n\nSoubory jsou zhodne\n");
    else
      printf("\n\nSoubory se lisi v %d znacich\n",count_of_differences);

    fclose(fr_pismena);
    fclose(fr_pismena2);

    getch();
    return 0;

}
</time.h></conio.h></stdio.h>
 
Share this answer
 
Comments
den2k88 15-Nov-14 17:21pm    
What problem did you have with '&&'? It should work fine, also it's not safe to use ungetc to undo a fscanf.
Total_Beginner 16-Nov-14 8:04am    
"Short - circuit evaluation" of '&&' will cause decrement -1 of count_of_differences, because '&&' firstly (!)reads and evaluates the first condition(!) and secondly evaluates the second condition. When second condition is evaluated as EOF, program continues with two 'while', where reads more two characters. So 1 character which was read in (!)....(!) is lost...

..or ? :)
den2k88 16-Nov-14 11:10am    
Ok, right I got it - that was pretty tricky indeed. Good solution!
Total_Beginner 16-Nov-14 12:14pm    
Thank you very much, also for the help :)

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