-2

I'm building a program that can read two files from the command line only once but I'm unable to read the other file when I try to read both files I get segmentation error.

char buffer[100], buffer2[100];

while (((fgets(buffer, sizeof(buffer), infp1)) && (fgets(buffer2, sizeof(buffer2), infp2))) != NULL)
{
    if(buffer[0] == '#')
        continue;
}

basically the above code takes two files when reading the first file and gets to a line with # it skips over it and continue to the next line.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
GeekyCoder
  • 67
  • 1
  • 11
  • 3
    Add the code where you're assigning `infp1` and `infp2`. – jmstoker Aug 01 '14 at 17:07
  • 4
    Hint: `&&` operator does short circuit evaluation. – sampathsris Aug 01 '14 at 17:07
  • `FILE *infp1, *infp2` – GeekyCoder Aug 01 '14 at 17:08
  • 3
    A short complete example showing how you open the files and such would help quite a bit. Think about what happens when one file is shorter than the other. You should also explain what your goal is rather than just throwing up some code and hoping we can figure it out. – Retired Ninja Aug 01 '14 at 17:08
  • There is nothing here that splits the input into lines. I suspect you simplified this a little too much for the example? – Dirk Groeneveld Aug 01 '14 at 17:08
  • 1
    What type is buffer? Are you allocating any memory there? If you haven't allocated memory for buffer, that is definitely going to crash. Also, sizeof(buffer) is not what you want for the 2nd argument. That's going to return the size of the pointer. – amo Aug 01 '14 at 17:09
  • Memory corruption? Check your buffer and buffer2 pointers, types and sizes. See [this answer](http://stackoverflow.com/a/2346849/3581917) for details on the segmentation fault. – Evil Dog Pie Aug 01 '14 at 17:11
  • @amo If `buffer` and `buffer2` are statically allocated arrays, then `sizeof` will work on them. – Jashaszun Aug 01 '14 at 17:11
  • 1
    `FILE *infp1, *infp2` is a declaration of those pointers. I'd like to see how you're assigning them, where the files are being opened. Post this code in the original question. Also note the other comments asking for more information. – jmstoker Aug 01 '14 at 17:19
  • OP has ignored requests for additional code needed to diagnose the problem. – chux - Reinstate Monica Aug 01 '14 at 18:24
  • The problem is the boolean expression which is wrong but not very visible because it is drowned in see of parenthesis. – Patrick Schlüter Aug 02 '14 at 08:30

1 Answers1

1

Your boolean expression is wrong

while (((fgets(buffer, sizeof(buffer), infp1)) && (fgets(buffer2, sizeof(buffer2), infp2))) != NULL)

You cannot factorize the comparison with NULL across a && operator.

What you did

while (
       ( 
        (fgets(buffer, sizeof(buffer), infp1)) && 
        (fgets(buffer2, sizeof(buffer2), infp2))
       ) != NULL
      )

You compared the result of a boolean expression (a && b) != NULL which is wrong. You put so many unecessary parenthesis that it's difficult to see what happens.

Change it to

while( fgets(buffer, sizeof buffer, infp1) != NULL &&
       fgets(buffer2,sizeof buffer2,infp2) != NULL)

Much more visbible what it is. Learn thoroughly the operator precedences and remove all unnecessary parenthesis, it helps a lot in readbility. Knowing also that sizeof and return are operators and not functions will help in not adding superfluous parens.

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48