0

I'm writing a C program, and it is dealing with fairly large files (~4MB .txt files). The program opens the big file and splits it up into a bunch of little files, before testing each of the little files. I've written a function that later opens those files, tests to make sure the full section was copied, and returns 1 if the section was not effectively copied (a different bug I'm having is that sometimes it only copies the first 2 words of a section). When I compile & run my program through koding.com (which uses the gcc compiler), it works perfectly for all test files. However, when I try to run it locally on my MacBook through Terminal (I run Lion, and have the version of gcc included in Xcode 4.6.3), it gives me "Segmentation fault: 11" and quits, but only when I use it on certain files (e.g. a 3.9MB file gives the segfault, but a 2.7MB file does not).

Here is how the function is called:

for(i=1;tableArray[i].count!=0;i++)
{
    strcpy(word,tableArray[i].shortName);
    strcat(word,".txt");
    if(fopen(word, "r")!=NULL)
    {
        testFile = fopen(word, "r");
        problems[i] = checkFile(testFile);
        fclose(testFile);
    }

}

And here is the function:

int checkFile(FILE *file)
{
    char word[NAMELEN];
    int count = 0;

    while(fscanf(file, "%s", word)!=EOF)
        count++;

    if(count<3)
        return(1);
    else return(0);
}

Any insight is much appreciated. Thanks!

awerchniak
  • 357
  • 3
  • 16
  • It's undefined behavior. Run your code with valgrind (or any other memory profiler of your choice) to see what's happening. – Sergey Kalinichenko Aug 16 '15 at 03:50
  • The loop index starting at `1` might be the problem. `for(i=1;tableArray[i].count!=0;i++)`. – R Sahu Aug 16 '15 at 03:52
  • Likewise may the possibility that `fscanf` is exceeding `NAMELEN`, which we naturally know nothing about (along with `word`, `problems`, etc..). Without code and data that *we* can use to replicate your problem, the best you can hope for are wags (wild-ass-guesses). – WhozCraig Aug 16 '15 at 03:55
  • I can't guess how big `i` and `count` numbers might get. I 'm suggesting to change them from `int` to `unsigned long long int` and check for results. Also about the `tableArray` you are skipping the `tableArray[0]` and its maximum possible index is not clear in the question. – Abcd Efg Aug 16 '15 at 05:29
  • @AbcdEfg Not `unsigned long long int` .. `size_t` is the sensible solution – Daniel Jour Aug 16 '15 at 05:53
  • @DanielJour `size_t` might look cleaner but they are the same thing. – Abcd Efg Aug 16 '15 at 06:19

0 Answers0