0

I am trying to make a program that prompts you to choose 1 or 2. 1 being to make a file and 2 being to append a file. Whenever I run the code for if (opt == 1) with the if it skips the prompt that asks about what the file name will be. I hope I'm being clear and you can understand my problem.

EXPECTED OUTPUT-

Enter a file name: (input)

Enter file contents: (input)

ACTUAL OUTPUT-

Enter a file name: Enter file contents: (input)

#include <stdio.h>

int main(void) {

    char fname[20];
    char fcontents[250];
    int opt;

    printf("Make a file(1),Append a file(2)");
    scanf("%d", &opt);

    if (opt == 1)
    {
        printf("Enter a file name: ");
        fgets(fname, 20, stdin);
        printf("Enter file contents: ");
        fgets(fcontents, 250, stdin);
        FILE* fpointer = fopen(fname, "w");
        fprintf(fpointer, "%s", fcontents);
        fclose(fpointer);
        printf("FILE: %s successfully created", fname);
    }
    else if (opt == 2) 
    {
        printf("Enter a file name: ");
        fgets(fname, 20, stdin);
        printf("Enter file appends: ");
        fgets(fcontents, 250, stdin);
        FILE* fpointer = fopen(fname, "a");
        fprintf(fpointer, "%s", fcontents);
        fclose(fpointer);
        printf("FILE: %s successfully created", fname);
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    The `scanf` function leaves the newline from your `Enter` key in the input buffer for the `fgets` call to read as an empty line. Don't mix `scanf` and `fgets`. In fact, `scanf` can cause more problems than it solves, use `fgets` to read the whole line, then use `sscanf` to parse the input. – Some programmer dude Apr 04 '23 at 16:45
  • Could you explain more about what I have to do? I'm kind of new to C and don't understand what you mean. –  Apr 04 '23 at 16:49
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Gerhardh Apr 04 '23 at 16:50
  • 1
    One debugger breakpoint and a single-step, and/or one well-placed `printf` of `fname` would expose where the problem is. The link provided by Gerhardh will tell you what is going on, and ways to fix it, though I think [this one](https://stackoverflow.com/questions/64880882/how-to-i-solve-this-issue-with-fgets-in-c) (also marked a duplicate) is more tightly related. This problem is posted by those new to C a dozen times a week, it seems. – WhozCraig Apr 04 '23 at 16:50
  • You could call `scanf(" ");` between the `printf()` in the `if` section and the first `fgets()` to eat any white-space that was left behind by `scanf("%d", &opt);` in the input (i.e. the newline character). (You would also need to do the same in the `else if` section.) – Ian Abbott Apr 04 '23 at 17:12

2 Answers2

1

After this call of scanf

scanf("%d", &opt);

the input buffer contains the new line character '\n' that corresponds to the pressed key Enter.

So the next call of fgets reads an empty string that contains this new line character.

A simple way to resolve the problem is to use the function scanf instead of fgets as for example

scanf( " %19[^\n]", fname );

If the file name can not contain spaces then it will be even simpler to write

scanf( "%19s", fname );

To read the content stored in the file it is better to write

scanf( " %249[^\n], fcontents );

Pay attention to the leading space in the format string. It allows to skip white space characters.

Another approach is to use everywhere the function fgets.

To get a value for the variable opt you can use in this case functions atoi or strtol that convert strings to integers.

Bear in mind that the function fgets also can append the read string with the new line character that you should remove at least in the variable fname. To do that you need to include header <string.h> and write for example

fgets( fname, sizeof( fname ), stdin );
fname[ strcspn( fname, "\n" ) ] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Try using getchar() after the scanf() function.

int opt;

printf("Make a file(1),Append a file(2)");

scanf("%d", &opt);
getchar();
user16217248
  • 3,119
  • 19
  • 19
  • 37
hsrh
  • 3
  • 2