Open In App

Removing trailing newline character from fgets() Input

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads