Friday, June 17, 2022
HomeWordPress DevelopmentEradicating trailing newline character from fgets() Enter

Eradicating trailing newline character from fgets() Enter


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

Nonetheless, fgets() additionally reads the trailing newline character and finally ends up returning the information string adopted by ‘n’. So, on this article, we’re going to see some strategies to take away trailing newline characters from fgets() enter. 

Strategies to take away trailing newline characters from fgets() enter

Technique 1(Utilizing strcspn() operate): 

It’s a C library operate that calculates the size of the variety of characters earlier than the first prevalence of a personality current in each the strings.

 Syntax: 

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

Parameters:
str : The string wherein knowledge has been saved utilizing fgets()  i.e.(char *fgets(char *str, int n, FILE *stream))
“n”: The character until which the size ought to be calculated

As a result of now we have given “n” as a second-string so we’ll get the size of the string earlier than the “n”. Now put the ‘ rather than ‘n

Beneath is the implementation:

C

#embrace <stdio.h>

#embrace <string.h>

#outline BUFFER_SIZE 15

int primary()

{

    char str[BUFFER_SIZE];

    printf("Enter the information = ");

    if (fgets(str, sizeof(str), stdin) == NULL) {

        printf("Fail to learn the enter stream");

    }

    else {

        str[strcspn(str, "n")] = '';

    }

    printf("Entered Knowledge = %sn", str);

    return 0;

}

Output:

Enter the information = 
Geeks
For
Geeks

Entered Knowledge = Geeks

Technique 2 (Utilizing strlen() operate):

On this technique, we’ll use strlen() operate to find out the size of the string wherein knowledge enter was taken by utilizing fgets() and we’ll manually change the char on the finish to null.

Beneath is the implementation:

C

#embrace <stdio.h>

#embrace <string.h>

#outline BUFFER_SIZE 15

int primary()

{

    char str[BUFFER_SIZE];

    printf("Enter the information = ");

    if (fgets(str, sizeof(str), stdin) == NULL) {

        printf("Fail to learn the enter stream");

    }

    else {

        str[strlen(str)] = '';

    }

    printf("Entered Knowledge = %sn", str);

    return 0;

}

However Issues could happen on this technique if the learn file by fgets() is a binary file or the primary char learn is null. As in that case, strlen(str) would return 0.

Technique 3 (Utilizing strchr() operate):

On this technique, we’ll use strchr() operate to interchange “n” in str with null, if “n” exists within the string.

Beneath is the implementation:

C

#embrace <stdio.h>

#embrace <string.h>

#outline BUFFER_SIZE 15

int primary()

{

    char str[BUFFER_SIZE];

    printf("Enter the information = ");

    if (fgets(str, sizeof(str), stdin) == NULL) {

        printf("Fail to learn the enter stream");

    }

    else {

        

        char* ptr = strchr(str, 'n');

        if (ptr) {

            

            *ptr = '';

        }

    }

    printf("Entered Knowledge = %sn", str);

    return 0;

}

Another features like strok and strok_r can be used protecting exceptions in thoughts.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments