2

I have code that prompts the user for a filename, then displays the contents of the file on the console. However, the printf() statement I use won't print out until the very end, so the user doesn't know to input a filename.

int main(int argc, char * argv[]){
printf("%s", "What file would you like to open?\t");
char filename[100];
scanf("%[^\n]", filename);
printf("You chose:\t%s\n", filename);
return 0;
}

The program currently scans the console, then prints out both printf() statements. The scanf() statement works correctly, just not at the right time. Any idea what my problem is?

EDIT: the program works correctly when run from the command line, but the problem persists in Eclipse.

nickhirakawa
  • 247
  • 1
  • 12

1 Answers1

4

Try adding fflush(stdout); after the first printf, I think the issue is with printf not flushing it's buffer.

EDIT: It seems that it's a known bug in Eclipse, the workaround would be to set the buffer size to zero (check the comments for the code).

lccarrasco
  • 2,031
  • 1
  • 17
  • 20