I am a complete beginner in C programming, so please have some patience with me. I am trying to input a file name from the user in the console and I want to print a helpful message that prompts the user to enter the filename he wants to open. But when I run this in the command prompt, the cursor waits for the input first and after I enter some text and hit Return, I see the helpful prompt that I wanted to print before the input. Here is the code snippet.
char filename[40];
fputs("enter the file name: ", stdout);
fflush(stdout);
fgets(filename, sizeof(filename), stdin);
I can’t see where I am going wrong with this. I would really appreciate if some one could explain why this happens.
I do not see any issues with the code you have pasted, works fine with gcc. It must be something to do with the stdout not being flushed, which could be specific to the compiler you are using…