-1

Here is my code:

#include <stdio.h>

int main() {    
    FILE *file;
    file = fopen("data.txt", "r");

    int Array[10];
    int i = 0;
    fscanf(file, "%d", &i);
    while (!feof(file)) {
        fArray[j] = i;
        j++;
        fscanf(file, "%d", &i);
    }
    fclose(file);

    return 0;
}

when I run this on windows, it works fine, but when I try to run it on a mac I get a segmentation fault : 11.

The data.txt file is in the same folder as my test.c file which code is shown above.

I tried to pinpoint where the segmentation fault occurs, and it is when I make the call to fscanf().

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 3
    Check that `file` is not equal to `NULL` after opening it. – IllusiveBrian Feb 20 '15 at 02:34
  • 3
    Have you pasted the code correctly? `fArray[j] = i;` seems like a typo and there's no declaration for `j`. – sonologico Feb 20 '15 at 02:34
  • 2
    The file could be more than 10 lines long, in which case Array would overflow. – Vedaad Shakib Feb 20 '15 at 02:41
  • 5
    [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). Also, copy and paste the code from your editor, this code cannot even be compiled. – Iharob Al Asimi Feb 20 '15 at 02:41
  • 1
    `gcc -Wall -Werror -g mycode.c && gdb ./a.out`, then `r` – Jonathon Reinhart Feb 20 '15 at 02:44
  • 1
    @iharob: actually, it's correct here: check eof _after_ fscanf and _before_ using that value... – FRob Feb 20 '15 at 02:45
  • @FRob it's unidiomatic though and forces code duplication, it's clearer and more maintainable to loop properly – M.M Feb 20 '15 at 02:46
  • @FRob yes like `while (fscan(...) == count)`. – Iharob Al Asimi Feb 20 '15 at 02:53
  • @FRob: `scanf()` can return 0 even when it isn't at EOF if there's an non-numeric character in the file. In that case, the `feof()` call is no help. There is _no_ substitute for checking the return value from `scanf()`; it is still wrong to use `feof(fp)` rather than checking that the I/O functions return successfully. – Jonathan Leffler Feb 20 '15 at 03:21

1 Answers1

1

So much potential fail in one small example:

#include <stdio.h>

int main() { 
    FILE *file;
    file = fopen("data.txt", "r");
    /* FAIL 1 - what if file is NULL? */
    int Array[10];
    /* fail 2 - bad naming convention - variables typically start with lower case (note: that's a lower case fail ;-) 
       fail 3 magic number 10 for array size
     */
    int i = 0;
    fscanf(file, "%d", &i);
    /* FAIL 4 - see iharob's comment on the question. I didn't pick this one up */
    while (!feof(file)) {
        /* FAIL 5 - compilation failure. What is j? - what is fArray?
                    (I'm assuming j is initialized, but since we don't know what it is...)
           FAIL 6 How do we know there isn't an array out of bounds access here
                  (as in j goes past the end of fArray) */
        fArray[j] = i;
        j++;
        /* FAIL 7 - if the file contains something other than an int then
         fscanf will return early and so you'll never hit EOF.
         Credit for this one goes to  rpattiso
         fail 8 (lowercase) - due of the feof style issue, you have to
         repeat the fscan line - once outside the loop and once inside.
         */

        fscanf(file, "%d", &i);
    }
    fclose(file);

    return 0;
}
John3136
  • 28,809
  • 4
  • 51
  • 69
  • what happens if `fscanf` fails to read an integer? ... FAIL 4 - infinite loop – ryanpattison Feb 20 '15 at 03:00
  • @JonathanLeffler your first and last items are covered in 'fail 3'. I think it's time to renumber the fails ;-) – John3136 Feb 20 '15 at 03:51
  • there are only integers in the file as this was for a test. there are the numbers 1,2,3,4,5. @John3136 so the array will not overflow. – maxence coulibaly Feb 23 '15 at 00:22
  • It also works fine on a PC, when I run it, if I try to print the numbers, everything shows up fine, but on a mac I get a segmentation fault. – maxence coulibaly Feb 23 '15 at 00:36
  • Have you even looked at any of the issues pointed out? Problem with opening file? *unexpected* buffer overruns etc? You're going to need to do some debugging to find out what is going on. – John3136 Feb 23 '15 at 00:44