简体   繁体   English

如何在C中的.txt文件中忽略换行符

[英]How do i ignore newline in .txt file in c

i am reading in from a text file and storing each element into a linked list. 我正在从一个文本文件中读取并将每个元素存储到一个链表中。 however when the program reaches a return char or newline the program returns an error. 但是,当程序到达返回字符或换行符时,程序将返回错误。

reading from text file and storing into structs. 从文本文件读取并存储到结构中。 I notice the error when i print each struct to the screen the program prints the last struct before the return char then quits. 当我将每个结构打印到屏幕上时,程序会在返回char之前打印最后一个结构,然后退出,这时我会注意到错误。

int main(int argc, const char * argv[]) {
    printf("Hello!\n");

    char filename[] = "artist.txt";

    print_artists(read_artists(filename));

    return 0;
}

    struct artist *read_artists(char *fname)
    {
        int maxlen = 225;

        int artid = 0;
        int altartid = 0;
        int pc = 0;
        char artname[80];

        char data[maxlen];
        int valid = 0; // 0 acts as true 1 acts as false
        int checkresult = 0; //checks result of sscanf

        struct artist *temphead = create_artist(0,0,0,"0");

        FILE *fp = fopen(fname ,"r");

        if (fp != NULL)
        {
            while (fgets(data,maxlen,fp))
            {
                checkresult = sscanf(data,"%d\t%[^\t\n]\n",&artid,artname);
                if (checkresult == 2)
                {
                    struct artist *b = NULL;
                    b = create_artist(artid,altartid,pc,artname);
                    temphead = add_artist(temphead,b);
                }
                else
                {
                    printf("error checkresult = %d\n",checkresult);
                    printf("break out of loop valid = 1\n");
                    valid = 1; // acts as boolean variable
                    break; //breaks out of the while(fgets)
                }
            }
            fclose(fp);
            return (temphead);
        }
        else
        {
            fclose(fp);
            printf("File Error\n");
            return (NULL);
        }

    }

void print_artists(struct artist *head)
{
    if (head != NULL)
    {
        struct artist *temp = head;
        while (temp -> next != NULL)
        {
            print_artist(temp);
            temp = temp -> next;
        }
        print_artist(temp);
    }
}

this is the output 这是输出

在此处输入图片说明

the text file i am reading from 我正在读取的文本文件

在此处输入图片说明

When you reach the blank line before Bodenstandig, the sscanf will not match and you will exit the while loop at the "break". 当您在Bodenstandig之前到达空白行时,sscanf将不匹配,并且您将在“中断”处退出while循环。

If sscanf does not return 2, you should check to see if the line is empty and if so skip and read the next line. 如果sscanf没有返回2,则应检查该行是否为空,如果是,请跳过并阅读下一行。 Or just skip the check and ignore bad lines. 或者只是跳过检查并忽略不良行。 The while(fgets(...) condition will become false at the end of the file, so you don't need to exit on a bad line. while(fgets(...)条件在文件末尾将变为false,因此您无需在错误的行上退出。

if (checkresult == 2)
{
    ....
}
else
{
    continue; // will go back to top while(fgets(...))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM