1

I am building an application that will need to add arabic characters in the middle of english file, I build the function as follow:

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;
}

for a text file as follows:

$ 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

and expecting to receive

$ 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

although when I put the arabic letter ASCII like:

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

I receive the following:

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

don't know why?!

1 Answers1

0

The function you are using for output will terminate at a null character. Instead you should use ostream::write for the output

outdata2.write(wbuffer, size);

Also since you are doing binary I/O all your files should be opened with the std::ios::binary bit set.

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

etc.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks for your answer, I tried the changes as follows and gives same result. – Mohsen Deeb Dec 20 '20 at 23:26
  • @MohsenDeeb I don't think I understand your problem. There are no Arabic characters in the file, only numbers and punctuation. – john Dec 21 '20 at 07:18