3

My program needs to take a user's input and save it to an external file for future reference. Here is the basic outline of the code.

void newActivity(FILE *foutput) {
    char name[31];
    char description[141];
    finput = fopen("activities.txt", "a");

    printf("\n What is the name of your activity (up to 30 characters):\n");
    fgets(name, sizeof(name), stdin);

    printf("\nEnter a brief description (up to 140 characters) of what %s is about:\n",
           fputs(name, stdout));
    fgets(description, sizeof(description), stdin);

    if (finput == NULL) {
        printf("\nCould not open file.");
        exit(1);
    }

    fprintf(foutfile, "%s\n", name);
    fprintf(foutfile, "%s\n", description);

    fclose(foutfile)
}

When I run a simple test program that only asks for a name and prints that name, all is good. It looks like this:

int main() {
    char name[50];
    fprint("What is your name? ");
    fgets(name, sizeof(name), stdin);
    fputs(name, stdout);
    return 0;
}

Unlike the working test program, my program does not not take any input from the user before moving to the second printf() statement. It does read the strings within the printf statements, but return a value of (null).

As for writing to the file, the two fprintf lines should do it, but I cannot confirm it as the input text has not been properly recorded.

This is a function declared outside of my main(). Does that make affect the situation?

3
  • 1
    Also, why the function has useless input parameter finput? Commented Jul 31, 2016 at 14:46
  • 5
    You're printfing strings name and description with the %decimal format specifier. That makes your program invoke undefined behaviour. It's possible that anything related to name and description have just been optimised away, or otherwise trashed. Have you tried using a debugger? Edit: As dasblinkenlight points out, that's not your only problem. Please think about what you're writing. Commented Jul 31, 2016 at 14:46
  • What is fprint("What is your name? ") - line 3 of main()? Where is newActivity() being called? Commented Jul 31, 2016 at 16:06

1 Answer 1

7

This is incorrect:

printf("\nEnter a brief description (up to 140 characters) of what %s is about:\n", fputs(name, stdout));

fputs returns an int, while your printf wants a string %s.

Remove fputs, and pass name to printf instead:

printf("\nEnter a brief description (up to 140 characters) of what %s is about:\n", name);

Use %s when writing out the string to a file:

fprintf(foutfile, "%s", name);
fprintf(foutfile, "%s", description);

Note that you don't need \n, because fgets keeps \n with the string.

From the comments: My concern is why the program is failing to [read input] for fgets(name, sizeof(name), stdin)

This commonly happens when your stdin has an extra \n lingering from a previous operation. For example, if your previous input operation has been reading of an int using scanf, you would see this effect:

scanf("%d", &num);
fgets(name, sizeof(name), stdin);

If the user pressed 5 Enter X Enter, the program would set num to 5, but name would be set to a string with a single '\n', rather than 'X'. This is because scanf fails to remove '\n' produced by Enter from the buffer, so scanf finds it, and thinks that the user just entered an empty string.

Sign up to request clarification or add additional context in comments.

4 Comments

While this is helpful information, it does not address the main issue: The program does not take any input from the user.
@Naltroc Why, it should! One thing to check is that there's no \n in the input buffer prior to calling newActivity. This may happen if you call scanf with %d, %f, or other numeric format specifier.
that does solve the problem regarding returning the proper string. Thank you for that. What I am still baffled by is line 7 of the first block of code! It does not take my input. Instead, it prints 9, runs line 10 and takes a longer string, and then returns that string as "%s", description, just as it should. My concern is why the program is failing to do this for fgets(name, sizeof(name), stdin).
@Naltroc Take a look at the edit, this should explain what is going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.