Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing persian ( farsi ) by class wfstream in output file

How can I write Persian text like "خلیج فارس" to a file using a std::wfstream?
I tried following code but it does not work.

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::wfstream f("D:\\test.txt", std::ios::out);
    std::wstring s1(L"خلیج فارس");
    f << s1.c_str();
    f.close();

    return 0;
}

The file is empty after running the program.

like image 944
SaeidMo7 Avatar asked Mar 14 '23 17:03

SaeidMo7


1 Answers

You can use a C++11 utf-8 string literal and standard fstream and string:

#include <iostream>
#include <fstream>

int main()
{
    std::fstream f("D:\\test.txt", std::ios::out);
    std::string s1(u8"خلیج فارس");
    f << s1;
    f.close();

    return 0;
}
like image 50
robsn Avatar answered Mar 17 '23 05:03

robsn