fgetwc EOF循环测试失败,但65535 OK

时间:2016-01-23 13:42:43

标签: c++ c loops testing eof

VS10& MCBS:

为此我创建了一个名为 c:\ eoftest 的文件,其中包含文本" test" 。以下代码中第5遍的ch值是fgetwc返回的65535,但它不等于EOF,我们都知道它在stdio.h中定义为(-1):

#include <stdio.h>
#include <windows.h>

int main()
{
    int ch;
    FILE *stream = NULL;
    wchar_t buf[5];
    memset (buf, '\0', sizeof(buf));
    stream = _wfopen(L"C:\\eoftest.txt", L"r");

            for (int i = 0; (i  < (sizeof(buf) - 1) && ((ch = fgetwc(stream)) != EOF) && (ch != '\0')); i++) //we are reading so last null condition mandatory
            {
            ungetwc(ch, stream);
            buf[i] = (wchar_t)(ch = fgetwc(stream));
            }
}

在这种情况下用(ch = fgetwc(stream)) != 65535)替换条件(原文如此),但是为了确保EOF测试能够成功,还没有做什么?

1 个答案:

答案 0 :(得分:1)

来自fgetc, fgetwc

的MSDN文档
  

fgetc 会将读取的字符作为 int 返回,或者返回 EOF 来表示错误或文件结束。 fgetwc 返回,作为    wint_t ,与读取的字符对应的宽字符   返回 WEOF 以指示错误或文件结尾。

WEOF定义为0xFFFF,这是您之前替换的内容65535

#define WEOF ((wint_t)(0xFFFF))

因此宽字符的EOF测试应更改为

if ((ch = fgetwc(stream)) != WEOF) ...

修改

int main()
{
    wchar_t buf[5];
    memset(buf, '\0', sizeof(buf));
    wcscpy(buf, L"1234");

    FILE *stream = _wfopen(L"C:\\eoftest.txt", L"rb");
    if (!stream)
    {
        stream = _wfopen(L"C:\\eoftest.txt", L"w+b");
        if (!stream)
        {
            printf("cannot create file\n");
            return 0;
        }

        fwrite((char*)buf, sizeof(buf[0]), wcslen(buf), stream);
        fseek(stream, 0, 0);
    }

    int len = sizeof(buf) / sizeof(buf[0]);
    for (int i = 0; i < len; i++) 
    {
        wchar_t ch = fgetwc(stream);
        if (ch == WEOF) break;
        buf[i] = ch;
    }

    wprintf(L"result = %s\n", buf);

    return 0;
}

编辑2:这将逐行打印unicode文件的内容:

int main()
{
    FILE *stream = _wfopen(L"c:\\test\\test.txt", L"rb");
    if (!stream) return 0;

    int buflen = 256;
    wchar_t* buf = (wchar_t*)malloc(buflen * sizeof(wchar_t));

    if (fread(buf, 2, 1, stream))
    {
        if (buf[0] != 0xFEFF)
        {
            //BOM not detected, go back to start of file
            rewind(stream);
        }//else, skip the first 2 bytes
    }

    int i = 0, line = 0;
    wint_t ch = 0;
    while (ch != WEOF)
    {
        ch = fgetwc(stream);
        if (ch == L'\n' || ch == WEOF)
        {
            //null-terminate the buffer at i
            buf[i] = L'\0';

            //trim the '\r' at the end, if any
            if (i > 0 && buf[i - 1] == '\r') buf[i - 1] = L'\0';

            wprintf(L"%3d %s\n", ++line, buf);

            //start a new line for the next pass
            i = 0;
        }
        else
        {
            buf[i] = ch;
            i++;
            if (i == buflen)
            {
                //increase buffer:
                buflen += 256;
                buf = (wchar_t*)realloc(buf, buflen * sizeof(wchar_t));
            }
        }
    }

    free(buf);
    return 0;
}