0

I have a specific problem. I have to read Strings from two files and compare them char by char, and tell in which row is the difference and for how many chars they differ. I have made pointer and all stuff, but the problem is when it comes to the end of the first file (it needs to read just for the length of the shorter file) I am unable to compare the last String because my for loop goes till the '\n', and in last row there is no \n. But on the other side if I put '\0' in for loop it gives me a wrong result, because it counts '\n' as char as well. How do I handle this problem? Suggestions? I don't want to use strcmp() since I need to count char difference, and there are some other conditions to be fulfilled. Here is my problem:

while(!feof(fPointer)){
    zeichnen = 0;
    fgets(singleLine, 150, fPointer);
    fgets(singleLine2, 150, fPointer2);
    length = strlen(singleLine);            // save the length of each row, in order to compare them
    length2 = strlen(singleLine2);
    if(length <= length2){
        for(i=0; singleLine[i] != '\n'; i++){
                singleLine[i] = tolower(singleLine[i]);
                singleLine2[i] = tolower(singleLine2[i]);
            if(singleLine[i] != singleLine2[i]){
                     zeichnen++;
                }
        }
        printf("Zeile: %d \t Zeichnen: %d\n", zeile, zeichnen);   // row, char
wildplasser
  • 43,142
  • 8
  • 66
  • 109
Buzz Black
  • 53
  • 1
  • 6
  • 1
    Note: there are **two** EOF conditions to handle. Note2: don't use `feof()` http://stackoverflow.com/q/5431941/905902 – wildplasser Oct 22 '16 at 12:37
  • I don't need two EOF conditions, since I've made comparison of files previous and decide which file do I include in while loop, this is just part of code where my problem is. – Buzz Black Oct 22 '16 at 12:40
  • Note3: always check return value of every IO function (except regular printing to stdio/stderr, because that wpuld get more tedious than it is worth). – hyde Oct 22 '16 at 12:40
  • @BuzzBlack `while (!feof(...))` is still wrong. – melpomene Oct 22 '16 at 12:42
  • Ok, I got it. `while(!feof(...))` is wrong, but how does that solve my problem – Buzz Black Oct 22 '16 at 12:44
  • Note4: `fgets()` returns a value (a string pointer, to be exact) You could use the two return values from the two fgets() calls. – wildplasser Oct 22 '16 at 12:46
  • @BuzzBlack No idea, because your problem description is unclear, there's no [sample code](http://stackoverflow.com/help/mcve), and no expected/actual output. – melpomene Oct 22 '16 at 12:48
  • the answer below works fine for me. Thanks anyway. Task is too long to describe everything, but appreciate your help. – Buzz Black Oct 22 '16 at 12:58

1 Answers1

0
for(i=0; i < min(length, length2); i++) {
      if ( singeLine[i] == '\n' || singleLine2[i] == '\n')
        break;
       /* Do work */
}       

Alternative is to use logical and operator to test two or more conditions in for loop conditional part.

From fgets manual page:

char *fgets(char *s, int size, FILE *stream);

fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read.

So you chould have while(true) loop and check both fgets return values for end of input.

Pauli Nieminen
  • 1,100
  • 8
  • 7