How to Free Memory in C Programming
How to Make a Simple Comparisons in C Programming
How to Create Functions that Return Values in C Programming

How to Use the fgets() Function for Text Input in C Programming

For a general-purpose text input function in the C programming language, one that reads beyond the first white space character, try the fgets() function. Here’s the format:

#include <stdio.h>
char * fgets(char *restrict s, int n, FILE *restrict stream);

Frightening, no? That’s because fgets() is a file function, which reads text from a file, as in “file get string.” That’s how programmers talk after an all-nighter.

Because the operating system considers standard input like a file, you can use fgets() to read text from the keyboard.

Here’s a simplified version of the fgets() function as it applies to reading text input:

fgets(string,size,stdin);

In this example, string is the name of a char array, a string variable; size is the amount of text to input plus one, which should be the same size as the char array; and stdin is the name of the standard input device, as defined in the stdio.h header file.

THE FGETS() FUNCTION READS A STRING

#include <stdio.h>
int main()
{
  char name[10];
  printf("Who are you? ");
  fgets(name,10,stdin);
  printf("Glad to meet you, %s.\n",name);
  return(0);
}

Exercise 1: Type the source code from The fgets() Function Reads a String into a new project, ex0716. Compile and run.

The fgets() function in Line 8 reads in text. The text goes into the name array, which is set to a maximum of ten characters in Line 5. The number 10 specifies that fgets() reads in only nine characters, one less than the number specified. Finally, stdin is specified as the “file” from which input is read. stdin is standard input.

The char array must have one extra character reserved for the \0 at the end of a string. Its size must equal the size of input you need — plus one.

Here’s how the program runs:

Who are you? Danny Gookin
Glad to meet you, Danny Goo.

Only the first nine characters of the text typed in the first line are displayed. Why only nine? Because of the string’s terminating character — the NULL, or \0. The room for this character is defined when the name array is created in Line 5. If fgets() were to read in ten characters instead of nine, the array would overflow, and the program could malfunction.

Exercise 2: Change the array size in the source code from The fgets() Function Reads a String to a constant value. Set the constant to allow only three characters input.

  • The fgets() function reads text from standard input, not from the keyboard directly.

  • The value returned by fgets() is the string that was input.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Basics of Sequential File Access in C Programming
How to Use Hex with Binary for C Programming
Basics of the scanf() Function in C Programming
How to Do Simple Arithmetic with C Programming
How to Use the Bitwise Exclusive OR (XOR) Operator in C Programming
Advertisement

Inside Dummies.com