Segmentation Fault in Terminal but not on koding.com

110 Views Asked by At

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!

0

There are 0 best solutions below