简体   繁体   English

C:EOF检测和fgetc

[英]C: EOF detection & fgetc

OBJECTIVE: How to implement EOF successfully to stop the infinite loop? 目标:如何成功实现EOF以阻止无限循环?

part where the function is called: 调用函数的部分:

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);

function definition: 功能定义:

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;
    */
}

sample input: 样本输入:

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

sample output: 样本输出:

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

My call is to look at return statement above. 我的电话是查看上面的return语句。 For some reason, it does not detect if it is EOF. 由于某种原因,它不会检测它是否是EOF。

Any help is appreciated! 任何帮助表示赞赏! Thanks! 谢谢!


updated: Thanks @JoachimPileborg! 更新:谢谢@JoachimPileborg! I have updated my code below: 我在下面更新了我的代码:

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

Check for EOF where you read from file, as 检查您从文件中读取的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;
}

You also have problems that mentioned in comments by @Joachim Pileborg 你也有@Joachim Pileborg评论中提到的问题

  • fgetc returns int not char fgetc返回int而不是char
  • You are returning bufferString which is local to function. 您正在返回bufferString ,它是函数的本地。

Can't write code in a comment so I post this as an answer. 无法在评论中编写代码,因此我将其作为答案发布。

You have (or at least had) this code in the question: 您在问题中拥有(或至少拥有)此代码:

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

In the first line above you allocate memory and makes pBufferString point to that memory. 在上面的第一行中,您分配内存并使pBufferString指向该内存。

In the second line you then make pBufferString point to the local array bufferString , so you no longer have a pointer to the memory you allocated with malloc , causing a memory leak (and a probable crash when you later try to free that pointer). 在第二行中,然后使pBufferString指向本地数组bufferString ,因此您不再具有指向使用malloc分配的内存的指针,从而导致内存泄漏(以及稍后尝试free该指针时可能发生的崩溃)。

You then return pBufferString , which is now pointing to the local array, leading to undefined behavior, as the stack-memory occupied by the local array is no longer valid after the function returns. 然后返回pBufferString ,它现在指向本地数组,导致未定义的行为,因为本地数组占用的堆栈内存在函数返回后不再有效。

Besides the above problem, you allocate almost four or eight times as much memory as needed. 除了上面的问题,你可以根据需要分配几乎四到八倍的内存。 The size of a pointer is four or eight bytes (depending on if you are on a 32 or 64 bit platform), but a char is only one byte. 指针的大小是四个或八个字节(取决于您是在32位还是64位平台上),但char只有一个字节。 It's enough to use strlen(bufferString) + 1 as the size to allocate: 使用strlen(bufferString) + 1作为要分配的大小就足够了:

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

You need the + 1 because the string terminator character is not included in the string length. 您需要+ 1因为字符串终止符不包含在字符串长度中。

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

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