1

目标:如何成功实现EOF来停止无限循环?

调用函数的部分:

do {
    pId = readInputField(fptr, DELIMITER_SPACE);
    pName = readInputField(fptr, DELIMITER_SEMICOLON);
    pDob = readInputField(fptr, DELIMITER_SPACE);
    pHobbyList = readInputField(fptr, DELIMITER_NEWLINE);
} while (NULL != pId
         && NULL != pName
         && NULL != pDob
         && NULL != pHobbyList);

函数定义:

char* readInputField(FILE* fPtr, const char delimiter) {
    int numCharRead;
    char bufferString[MAX_LENGTH_INPUT];
    char *pBufferString;

    numCharRead = 0;

    // flush: if spaces are found
    ' ' == (bufferString[numCharRead] = fgetc(fPtr)) ? 0 : numCharRead++;

    // get chracter array before delimiter
    while (delimiter != bufferString[numCharRead - 1]
            && numCharRead < MAX_LENGTH_INPUT) {
        bufferString[numCharRead++] = fgetc(fPtr);
    }

    // exclude delimiter from the string
    bufferString[numCharRead - 1] = '\0';

    printf("numCharRead=  \"%d\"\n", numCharRead);

    printf("delimiter:    \"%c\"\n", delimiter);
    printf("bufferString: \"%s\"\n", bufferString);

    pBufferString = malloc(sizeof(char*) * strlen(bufferString));

    /* deleted:
    pBufferString = bufferString;
    return EOF == bufferString[numCharRead - 1] ? NULL : pBufferString;
    */
}

样本输入:

VIC Lee, Victoria; 02/25/90 Knitting;Photography;Dance;

样本输出:

numCharRead=  "4"
delimiter:    " "
bufferString: "VIC"
numCharRead=  "14"
delimiter:    ";"
bufferString: "Lee, Victoria"
numCharRead=  "9"
delimiter:    " "
bufferString: "02/25/90"
numCharRead=  "28"
delimiter:    "
"
bufferString: "Knitting;Photography;Dance;"

// after this, infinite loop begins with garbage data

我的电话是查看上面的 return 语句。由于某种原因,它不会检测它是否是 EOF。

任何帮助表示赞赏!谢谢!


更新: 感谢@JoachimPileborg!我在下面更新了我的代码:

  // check for EOF
    if(bufferString[numCharRead-1] == EOF) {
        return NULL;
    } else {
        pBufferString = malloc(sizeof(char*));
        strcpy(pBufferString, bufferString);
        return pBufferString;
    }
4

2 回答 2

2

检查EOF您从文件中读取的位置,如

// flush: if spaces are found
' ' == (bufferString[numCharRead] = fgetc(fPtr)) ? 0 : numCharRead++;
if(bufferString[numCharRead-1] == EOF)
    return NULL;

while (delimiter != bufferString[numCharRead - 1]
            && numCharRead < MAX_LENGTH_INPUT) {
    bufferString[numCharRead++] = fgetc(fPtr);
    /* check for EOF */
    if(bufferString[numCharRead-1] == EOF)
        return NULL;
}

您还遇到@Joachim Pileborg 评论中提到的问题

  • fgetcint不返回char
  • 您正在返回bufferString本地功能。
于 2013-02-12T08:23:34.030 回答
1

无法在评论中编写代码,因此我将其发布为答案。

您在问题中有(或至少有)此代码:

pBufferString = malloc(sizeof(char*) * strlen(bufferString));
pBufferString = bufferString;
return EOF == bufferString[numCharRead - 1] ? NULL : pBufferString;

在上面的第一行中,您分配内存并pBufferString指向该内存。

然后在第二行中,您pBufferString指向本地数组bufferString,因此您不再有指向您分配的内存的指针malloc,从而导致内存泄漏(并且当您稍后尝试指向free该指针时可能会崩溃)。

然后你 return pBufferString,它现在指向本地数组,导致未定义的行为,因为本地数组占用的堆栈内存在函数返回后不再有效。

除了上述问题之外,您分配的内存几乎是所需内存的四到八倍。指针的大小为 4 或 8 个字节(取决于您是在 32 位还是 64 位平台上),但 achar只有一个字节。strlen(bufferString) + 1用作分配的大小就足够了:

pBufferString = malloc(strlen(bufferString) + 1);

您需要 ,+ 1因为字符串终止符不包含在字符串长度中。

于 2013-02-12T09:13:03.287 回答