fgets() 保存一个跳过的行字符,而不是跳过输入

fgets() saving a skipping line char, not skipping inputs

我知道在处理 fgets() 的跳过输入方面存在问题;但是我没有在网上找到任何关于这个问题的帮助,也没有问周围的朋友。

我使用:

fgets(tdataMplMsgStrng0_au8, 100, fpread);

从文本文件中读取字符串并将其分配给字符串tdataMplMsgStrng0_au8。在此之前和之后我有很多fgets()并且none正在跳过数据;但是其他的都在之后转换为整数,如下所示:

fgets(temp_s32, 100, fpread);
sint32 tCILXIOD_dataMplMsgDisp4To0_u32 = atoi(temp_s32);

问题是,当我将此字符串打印到文本文件时,它会跳过一个额外的行(除 \n 之外),其他行的 none 具有相同的格式:

fprintf(fp, "%s\n", tdataMplMsgStrng0_au8);

我打印了字符串中的每个字符并注意到它有一个 "skipped line" 作为最后一个字符的字符。我还看到一个字符,14 及以后,我不熟悉。

要打印到命令的代码 window:

    for (int i = 0; i < 100; i++)
    {
        printf("\n String[%d]: '%c'", i, tdataMplMsgStrng0_au8[i]);
    }

命令 window 的输出(注意文本文件包含设置 tdataMplMsgStrng0_au8 的行的“0jkhjkhkkljh”):

String[0]: '0'
String[1]: 'j'
String[2]: 'k'
String[3]: 'h'
String[4]: 'j'
String[5]: 'k'
String[6]: 'h'
String[7]: 'k'
String[8]: 'k'
String[9]: 'l'
String[10]: 'j'
String[11]: 'h'
String[12]: '
'
String[13]: ' '
String[14]: '|['
String[15]: '|['
String[16]: '|['

任何有助于理解索引 12 中的 "skipping line" 字符存在的原因的信息都会非常有帮助。同样只是为了获得额外信息,很高兴知道为什么在我没有输入时“|[”在那里。

fgets char * fgets ( char * str, int num, FILE * stream ); Get string from stream Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

它在规格中。

http://www.cplusplus.com/reference/cstdio/fgets/