简体   繁体   English

C-从文本文件中读取字符串并按大小排列

[英]C - Reading strings from a text file and arranging them by size

I want to read strings from a text file (one string/word per line) and then arrange them by size. 我想从文本文件中读取字符串(每行一个字符串/单词),然后按大小排列它们。

This is my code: 这是我的代码:

void readDic(char* file)
{
    FILE* fr; 
    fr=fopen(file, "rt"); // opening the text file
    char line[MAX_LINE_SIZE];
    char* word;
    while(fgets(line, MAX_LINE_SIZE, fr)!=NULL)
    {
        if(line[0]!='\n')
        {
            word = strtok(line, "\n"); //remove the newline from the string
            // do stuff with word
        }
    } 
    fclose(fr);
}

Although this code code runs, every string I read, except the last one, comes with a size of +1 than the one in the file. 尽管此代码可以运行,但我读取的每个字符串(最后一个字符串除外)的大小都比文件中的字符串大+1。

For example, strlen of the string "hello" returns 6 if its anywhere except the last lineof the file. 例如,如果字符串“ hello”的strlen除了文件的最后一行,则返回6。 If it is in the last line of the file strlen returns 5. 如果在文件的最后一行,则strlen返回5。

Am I doing something wrong? 难道我做错了什么?

fgets() does not read C strings. fgets()不读取C字符串。 It reads chars until encounters a '\\n' ( or EOF condition, or IO error or the buffer is nearly filled). 它读取chars直到遇到'\\n' (或EOF条件,或IO错误或缓冲区几乎已满)。 Then it appends a '\\0' to the buffer, making the buffer a C string. 然后,它将'\\0'附加到缓冲区,使缓冲区成为C字符串。

After calling fgets() , good to check its return value - which this code did. 调用fgets() ,可以检查其返回值-此代码可以完成此操作。 If NULL , an EOF condition or IO Error exist. 如果为NULL ,则存在EOF条件或IO错误。 Otherwise the buffer contains a C string: 1) an array of char , 2) typically the last is a '\\n' and 3) the appended '\\0' . 否则,缓冲区包含一个C字符串:1)一个char数组,2)通常最后一个为'\\n' ,3)附加的'\\0'

The seemingly extra long result of strlen(line) is from the '\\n' , which the last line of your text file does not have. strlen(line)看似多余的结果是来自'\\n' ,而文本文件的最后一行则没有。

Suggestion to rid the maybe trailing '\\n' : 建议删除可能结尾的'\\n'

size_t len = strlne(line);
if (len > 0 && line[len-1] == '\n') line[--len] = '\0';

Line endings vary between systems: "\\r\\n" and "\\n" are popular, but "\\n\\r" and "\\r" have occurred. 系统之间的行尾不同: "\\r\\n""\\n"很流行,但是出现了"\\n\\r""\\r" By opening the file as a text file "rt" , or more portable with "r" , the system's typical line ending is converted to "\\n" as fgets() requests data from the underlying IO. 通过将文件打开为文本文件"rt"或更具"r" ,当fgets()向底层IO请求数据时,系统的典型行尾将转换为"\\n" Given varieties amongst editors, it is possible that the text file that code is reading is using an unexpected line ending and is not translated as mentioned. 给定不同的编辑器,代码正在读取的文本文件可能使用了意外的行尾,并且未按所述进行翻译。

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

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