简体   繁体   English

无法停止使用!feof()读取Unicode文件

[英]Can't stop reading Unicode file with !feof()

I don't know why while loop can't stop. 我不知道为什么while循环无法停止。 It can't compare c with Delim neither stop by reaching eof. 它无法将c与Delim进行比较,也无法通过达到eof而停止。

wchar_t* Getline(const wchar_t* Filename, const wchar_t Delim){

FILE* f = _wfopen(Filename, L"r, ccs=UTF-8");

wchar_t* info = NULL;
wchar_t* temp = NULL;
int count = 1;
int i = 0;
wchar_t c;

c = fgetwc(f);

while (c != Delim || !feof(f))
{
    count++;
    temp = (wchar_t*)realloc(info, count * sizeof(wchar_t));

    if (temp)
    {
        info = temp;
        info[i] = c;
        i++;
    }

    else
    {
        free(info);
        wprintf(L"Failed to read\n");
    }
    c = fgetwc(f);
}
info[i] = '\0';

fclose(f);
return info;

} }

After reading all character in file. 读取文件中的所有字符后。 It seem not to stop. 似乎没有停止。 Even c are the same with Delim. 即使是c也与Delim相同。 And !feof(f) hasn't worked too. !feof(f)也不起作用。 I have try c != WEOF but fail too 我尝试了c!= WEOF但也失败了

I thought that the problem is in the file that I read but not. 我认为问题出在我读取的文件中,但没有。 I have change another file but the same problem. 我已经更改了另一个文件,但存在相同的问题。 Thanks for helping me! 感谢您的帮助!

You wish to loop whilst you have not got a Delim character and it is not the end of the file, so replace the || 您希望在没有Delim字符不是文件结尾的情况下循环,因此请替换|| with && &&

while (!feof(f) && c != Delim)

Edit: the order has also been changed in response to comments 编辑:响应评论的顺序也已更改

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

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