c++ - How to write Special characters in a file using mfc application? -
i want write special charaters ô. ö‚`a¹½ˆ in file. working in mfc , using unicode character set. while showing string in message box working not writing characters file.
here parts of code:
cstring abc=_t("hello"); cstring xyz=compress(abc); //compressing value , return special characters cstdiofile file_object(_t("abc.txt"),cfile::modecreate|cfile::modewrite); file_object.writestring(xyz);
it seems cstdiofile
class not support unicode characters directly. can use workaround (from this codeproject article)
// open file specified encoding file *fstream; errno_t e = _tfopen_s(&fstream, _t("abc.txt"), _t("wt,ccs=utf-8")); if (e != 0) return; // failed.. cstdiofile f(fstream); // open file stream f.writestring(xyz); f.close(); // // reading // // open file specified encoding file *fstream; errno_t e = _tfopen_s(&fstream, _t("abc.txt"), _t("rt,ccs=utf-8")); if (e != 0) return; // failed..cstring sread; cstdiofile f(fstream); // open file stream cstring sread; f.readstring(sread); f.close();
instead of using "utf-8" encoding, can use following encodings:
“ccs=unicode” => utf-16 (big endian)
“ccs=utf-8” => utf-8
“ccs=utf-16le” => utfs-16le (little endian)
“ccs=ansi” => ansi (default encoding of os)
Comments
Post a Comment