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?
finput?printfing stringsnameanddescriptionwith the%decimal format specifier. That makes your program invoke undefined behaviour. It's possible that anything related tonameanddescriptionhave 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.fprint("What is your name? ")- line 3 ofmain()? Where isnewActivity()being called?