Ok, so I’m not really sure what is going on here. I have a simple function, int foo(char *filename), that takes filename and counts the words in a file.
int foo(char *filename){
FILE *inFile;
int wordCount = 0;
printf("foo\n"); // test printf() statement (currently prints)
char word[50];
inFile = (&filename, "r");
printf("infile\n"); // test printf() statement (currently prints)
while (1){
printf("while"); // test printf() statement (doesn't print)
fscanf(inFile, "%s", word);
if (feof(inFile))
break;
printf("%d", wordCount); //test printf() statement
wordCount++;
}
fclose(inFile);
return wordCount;
}
As you can see, I print “infile”, but not “while”. I get a segmentation fault. Does anyone have any idea why this doesn’t work Also, is my inFile = (&filename, "r"); statement correct? I’m not that great with pointers.
I’m surprised this line actually compiles:
If you’re trying to open a file:
EDIT:
And as mentioned, you need to end your
printfs with\nor callfflush(stdout)or it will get buffered and not print.