Search code examples
cunicodefeof

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


I don't know why while loop can't stop. It can't compare c with Delim neither stop by reaching 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. And !feof(f) hasn't worked too. I have try c != WEOF but fail too

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!


Solution

  • You wish to loop whilst you have not got a Delim character and it is not the end of the file, so replace the || with &&

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

    Edit: the order has also been changed in response to comments