如何在.txt文件中保存泰语

时间:2014-01-01 02:22:38

标签: c++ visual-studio-2008 mfc text-files

我想在Edit Control中将文本作为泰语字符保存在.txt文件中。问题是在.txt文件中,泰语字符显示为???,但英文字符和数字没有问题。

我该怎么做?

char* getStringFromCString(CString& x) // code convert string
{
    char * temp = new char[(x.GetLength()) + 1];
    int i = 0;
    for (; i < x.GetLength(); i++)
    {
        temp[i] = x[i];
    }
    temp[i] = '\0';
    return temp;
}

void CPictureDlg::OnBnClickedButtonSave() // Save.txt files
{
    CString ss[2];
    CITIZEN_ID.GetWindowTextW(ss[0]);
    Name_text.GetWindowTextW(ss[1]);
    char* citizen = getStringFromCString(ss[0]);
    char* name = getStringFromCString(ss[1]);
    std::ofstream myfile;
    myfile.open("example.txt", std::ios::out | std::ios::app);
    myfile << citizen << "\t" << name;
    myfile.close();
    UpdateData(FALSE);
}

1 个答案:

答案 0 :(得分:1)

  • 问题

首先,您必须使用Unicode。请阅读此article。 其次,你的getStringFromCString()有一个发生内存泄漏的问题。

  • 方法

使用Unicode,std:wofstreamstd:locale用于泰语字符集。

std::wofstream myfile;

//I just checked for Korean, you may need std::locale("thai").  
myfile.imbue(std::locale("kor"));

myfile.open("example.txt", std::ios::out | std::ios::app);
myfile << input.GetString();
myfile.close();

我希望这会对你有所帮助。新年快乐〜

相关问题