I tested a piece of code to figure out what's causing the segmentation fault with the 'load' function. The code below opens the large dictionary. Then it scans and prints each string inside the dictionary. My problem is that this code is causing a segmentation fault:
int main(int argc, char* argv[])
{
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
return 1;
}
char buffer[27];
while (fscanf(file, "%s", buffer) != EOF)
{
printf("%s\n", buffer);
}
}
However, if I replace char buffer[27];
with `char *buffer = malloc(sizeof(char)*27);' (and call free at the end), the code works fine. Shouldn't it work fine with the array too? What am I missing here?
Thanks in advance