C++

How to Use fgets() in C++?

In C++, reading input from the user or from a file is a crucial part of many programs. One of the most famous functions utilized for input is fgets(). This function allows you to read input from a file or the user’s keyboard, and store it in a buffer.

In this article, we will explore how to use fgets() in C++, covering its syntax, parameters, and practical usage examples.

What is the fgets() Function in C++?

In C++, a string can be read from a designated input stream and stored in a string pointer using the fgets() function. The function terminates when it encounters any one of the following conditions:

  • It reads n-1 characters (where n is the size of the buffer passed as an argument).
  • It encounters a newline character (\n).
  • It reaches the end-of-file (EOF) indicator.

If the function encounters a newline character, it stops reading the input and includes the newline character in the text that gets copied to the string.

When the characters are copied to the string, the function automatically appends a null character (\0) at the end, making it a null-terminated string.

Syntax for fgets() Function in C++

The C++ fgets() function has the following syntax:

char* fgets(char* str, int count, FILE* stream);

Arguments: The C++ fgets() function accepts three arguments which are described below:

  • str: A pointer to the character array containing the file’s┬ácontent.
  • count:A maximum number of characters that can be written in the string str.
  • stream: The file stream from which the characters are read.

Return value: The C++ fgets() function returns the following:

  • The fgets() function returns string str on success and a null pointer on failure.
  • If a failure is caused by an end-of-file condition, the EOF indicator is set. The contents of the string do not change in this case.
  • If a failure is caused by another error, the error indicator is set. The contents of the string are undetermined in this case. They may not be null-terminated at all.

How Does C++ fgets() Function Work?

The fgets() function reads the maximum number of n-1 characters from the specified file stream and puts them in the string pointer. The parsing process continues until the end of file is reached or the newline character (\n) is encountered. Once a newline character appears, it will be included in the string pointer. If there are no errors; the string appears with the null character.

Example

Consider the following C++ program which implements the fgets() function:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int num_of_char = 100;
    char strng[100];
    FILE *file;
    file = fopen("myfile.txt", "r");
    while(feof(file) == 0)
    {
        fgets(strng, num_of_char, file);
        cout<<"Content read from the file is:\n";
        cout << strng << endl;
    }
    fclose(file);
    return 0;
}

The above code can read 100 characters from the text file named myfile.txt and stores them in the string pointer strng using the fgets() function. The resulting output is given below:

Output

Conclusion

In C++, a line of a string can be read from a designated input stream and stored in a string pointer using the fgets() function. It continues reading characters from the input stream until one of three conditions is met – either (n-1) characters have been read, a newline character is encountered, or the end-of-file character is reached, whichever occurs first. In this tutorial, we illustrated a detailed discussion of the fgets() function in C++.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.