I got this program below, but the fgets function did not work...
What's wrong with this?

#include<stdio.h>
main()
{
char name[30];
FILE *fp;
fp=fopen("c:\\file.txt","a+");
printf("enter name:");
fgets(name,50,fp);
return 0;
}

Recommended Answers

All 3 Replies

How do you know it didn't work?

suppose to be, the program will ask the user to input his name. But, I cant input anything on the program...

Try the code below

#include<stdio.h>

int main()
{
  char name[30];
  
  
  printf("enter name:");
  fgets(name,50,stdin);
  
  printf(name);

  return 0;
}

The code you posted worked, it just got the name from the file. This one gets it from the stdin(keyboard).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.