无法从输入文件中的C ++中的文件中读取utf-8字符

时间:2019-06-08 06:08:35

标签: c++ linux

我想从Linux平台的C ++程序的文件中读取utf-8字符。在fgets()函数中,它返回垃圾字符代替utf-8字符。 input.txt包含文本triuöschen

#include <stdio.h>
#include <string>

int main()
{
    FILE* fpointer = NULL;
    std::string szFileData = "";
    char cLine[1025] = "\0";
    int iTrailingPointer = 0;
    try {

        const char* pcFileName = "input.txt";
        //OPEN THE FILE IN READ MODE...
        fpointer = fopen(pcFileName, "r");
        if (fpointer == NULL)
        {
            printf("\n Error reading file %s", szFileData.c_str());
            return 0;
        }
        //READ THE FILE DATA TILL THE END OF FILE...
        while (!feof(fpointer))
        {
            memset(cLine, '\0', 1024);
            fgets(cLine, 1024, fpointer);
            iTrailingPointer = (int)strlen(cLine) - 1;

            //REMOVE TRAILING SPACES AND NEWLINES...
            while (cLine[iTrailingPointer] == '\n' || cLine[iTrailingPointer] == ' ' ||
                cLine[iTrailingPointer] == '\t')
            {
                iTrailingPointer--;
            }

            cLine[iTrailingPointer + 1] = '\0';
            szFileData = szFileData + cLine;
            printf("\n szFileData: %s", szFileData.c_str());
        }
        fclose(fpointer);

    }
    catch (...) {

    }
    return 0;
}

0 个答案:

没有答案