Skip to content
Related Articles

Related Articles

Removing trailing newline character from fgets() Input

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 16 Jun, 2022

fgets() reads a line from the specified stream and stores it into the string pointed by str. It stops when either (n – 1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

However, fgets() also reads the trailing newline character and ends up returning the data string followed by ‘\n’. So, in this article, we are going to see some methods to remove trailing newline characters from fgets() input. 

Methods to remove trailing newline characters from fgets() input

Method 1(Using strcspn() function): 

It’s a C library function that calculates the length of the number of characters before the 1st occurrence of a character present in both the strings.

 Syntax: 

str [ strcspn (str, “\n”)] = 0;

Parameters:
str : The string in which data has been stored using fgets()  i.e.(char *fgets(char *str, int n, FILE *stream))
“\n”: The character till which the length should be calculated

Because we have given ā€œ\nā€ as a second-string so we will get the length of the string before the “\n”. Now put the ā€˜\0ā€™ in place of ‘\n

Below is the implementation:

C




#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 15
int main()
{
    char str[BUFFER_SIZE];
    printf("Enter the data = ");
    if (fgets(str, sizeof(str), stdin) == NULL) {
        printf("Fail to read the input stream");
    }
    else {
        str[strcspn(str, "\n")] = '\0';
    }
    printf("Entered Data = %s\n", str);
    return 0;
}


Output:

Enter the data = 
Geeks
For
Geeks

Entered Data = Geeks

Method 2 (Using strlen() function):

In this method, we will use strlen() function to determine the length of the string in which data input was taken by using fgets() and we will manually change the char at the end to null.

Below is the implementation:

C




#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 15
int main()
{
    char str[BUFFER_SIZE];
    printf("Enter the data = ");
    if (fgets(str, sizeof(str), stdin) == NULL) {
        printf("Fail to read the input stream");
    }
    else {
        str[strlen(str)] = '\0';
    }
    printf("Entered Data = %s\n", str);
    return 0;
}


But Problems may occur in this method if the read file by fgets() is a binary file or the first char read is null. As in that case, strlen(str) would return 0.

Method 3 (Using strchr() function):

In this method, we will use strchr() function to replace “\n” in str with null, if “\n” exists in the string.

Below is the implementation:

C




#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 15
int main()
{
    char str[BUFFER_SIZE];
    printf("Enter the data = ");
    if (fgets(str, sizeof(str), stdin) == NULL) {
        printf("Fail to read the input stream");
    }
    else {
        // find new line
        char* ptr = strchr(str, '\n');
        if (ptr) {
            // if new line found replace with null character
            *ptr = '\0';
        }
    }
    printf("Entered Data = %s\n", str);
    return 0;
}


Some other functions like strok and strok_r can also be used keeping exceptions in mind.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!