提问者:小点点

如何使用C++在文件中间添加阿拉伯字符?


我正在建立一个应用程序,将需要添加阿拉伯字符在中间的英语文件,我建立的功能如下:

int main(void) {

    std::ifstream mySource("Test2.txt", std::ios::out);
    std::filebuf* pbuf = mySource.rdbuf();
    std::size_t size = pbuf->pubseekoff(0, mySource.end, mySource.in);
    pbuf->pubseekpos(0, mySource.in);
    char* buffer = new  char[size];
    pbuf->sgetn(buffer, size);
    mySource.close();

    wchar_t* wbuffer = new wchar_t[size];
    wbuffer = GetWC(buffer);

    setlocale(LC_ALL, "en_GB.utf8");
    wbuffer[79] = {0x0041};
   
    std::wofstream outdata2;
    outdata2.open("Test6.xml"); // opens the file
    outdata2 << wbuffer;
    outdata2.close();
  
    return 0;
}

对于文本文件,如下所示:

$ cat dat/rbgtst.txt
400,280: (234,163,097) #EAA361
400,300: (000,000,000) #000000
400,320: (064,101,160) #4065A0
400,340: (220,194,110) #DCC26E

并期待收到

$ cat dat/rbgtst.txt
400,280: (234,163,097) #EAA361
400,300: (000,000,000) #A00000
400,320: (064,101,160) #4065A0
400,340: (220,194,110) #DCC26E

虽然当我把阿拉伯字母ASCII像:

...
wbuffer[79] = {0x0628};
...

我收到以下资料:

$ cat dat/rbgtst.txt
400,280: (234,163,097) #EAA361
400,300: (000,000,000) #

不知道为什么?!


共1个答案

匿名用户

用于输出的函数将终止于空字符。相反,您应该使用作为输出

outdata2.write(wbuffer, size);

另外,由于您正在执行二进制I/O,所有文件都应该在设置位的情况下打开。

std::ifstream mySource("Test2.txt", std::ios::out|std::ios::binary);

等等。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|文件|添加|阿拉伯|字符)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?