You are here:

C/How to use fgets

Advertisement


Question
How to use fgets() to store the data?
I can use scanf() but I don't know how to use fgets() can you help me. I want to see Example program

Answer
googol:temp> cat temp.c
#include <stdio.h>

main()
{
       FILE *fp = fopen("temp_file", "r");
       char str[100] = "";

       fgets(str, 25, fp);

       printf("The string is: %s\n", str);

       fclose(fp);
}
googol:temp> cat temp_file
This is a temp string.
googol:temp> gcc temp.c
googol:temp> ./a.out
The string is: This is a temp string.

googol:temp>

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.