fgets() includes new line in string

2019-03-07 03:13发布

I get the words from my document extracted and all are printed on screen, but after each word printed there is a blank line. How can I avoid reading or adding this new line to the string?

int main(void) {
    FILE *f;
    f = ("words", "r");
    char string[100];
    while (fgets(string, 100, f)) {
         printf("%s", string);
    }
}

This code was not copy pasted, so I could have forgotten tiny pieces but should work. In words.txt I have one word on each line. My program prints them all to screen, but adds a new line after each word. I do not want it to add a new line, or a space. So if the txt had Hello on one line and Bye on the next line, I want it to print HelloBye. The objective of the final program will not be to print the string, it will be to use the string for something else, so I do need a string that only has the text without spaces at the end or new lines.

标签: c fgets
3条回答
唯我独甜
2楼-- · 2019-03-07 03:16

You cannot prevent fgets() from storing the '\n' at the end of the array supplied to it. If the array is too short, fgets() will not store the newline, it will leave the characters that do not fit in the array in the input stream to be read by the next input operation.

There are multiple ways to remove the trailing newline, which may or may not be present:

Using strcspn():

#include <stdio.h>
#include <string.h>

int main(void) {
    char string[100];
    FILE *f = fopen("words", "r");
    if (f != NULL) {
        while (fgets(string, sizeof string, f)) {
            string[strcspn(string, "\n")] = '\0';
            printf("%s", string);
        }
    }
    return 0;
}

Using strlen():

#include <stdio.h>
#include <string.h>

int main(void) {
    char string[100];
    FILE *f = fopen("words", "r");
    if (f != NULL) {
        while (fgets(string, sizeof string, f)) {
            size_t len = strlen(string);
            if (len > 0 && string[len - 1] == '\n')
                string[--len] = '\0';
            printf("%s", string);
        }
    }
    return 0;
}

Using strchr():

#include <stdio.h>
#include <string.h>

int main(void) {
    char string[100];
    FILE *f = fopen("words", "r");
    if (f != NULL) {
        while (fgets(string, sizeof string, f)) {
            char *p = strchr(string, "\n");
            if (p != NULL)
                *p = '\0';
            printf("%s", string);
        }
    }
    return 0;
}
查看更多
beautiful°
3楼-- · 2019-03-07 03:17

Try this.

int main()
{
    FILE *f;
    f = ("words", "r");
    char string[100];
    while (fgets(string, 100, f))
    {
        char * message = strtok(string, "\n");
        printf("%s", message);
    }
}

strtok tokenizes the string into your message, \n. fgets will capture the \n token

查看更多
ら.Afraid
4楼-- · 2019-03-07 03:22

By design fgets reads a line (terminated with a \n) and keeps the \n in the line - provided the buffer was big enough. Just look if last character is a \n, and it is it replace it with a \0, actually reducing the len of line by one:

int main(void)
{
    FILE *f;
    f = ("words", "r");
    char string[100];
    while (fgets(string, 100, f))
    {
         if ((string[0] != '\0') && (string[strlen(string) -1] == `\n')) {
             string[strlen(string) -1] = `\0';
         }
         printf("%s", string);
    }
}
查看更多
登录 后发表回答