Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Source: https://cppcodetips.wordpress.com/2014/09/16/reading-and-writing-a-unicode-file-in-c/
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <tchar.h>
- using namespace std;
- std::wstring readUnicodeFile(const char* filename)
- {
- std::ifstream wif(filename);
- std::stringstream wss;
- wss << wif.rdbuf();
- std::string const &str = wss.str();
- std::wstring wstr;
- wstr.resize(str.size() / sizeof(wchar_t));
- std::memcpy(&wstr[0], str.c_str(), str.size()); // copy data into wstring
- return wstr;
- }
- void WriteUnicodetoFile(const char* myFile, wstring& ws) {
- std::ofstream outFile(myFile, std::ios::out | std::ios::binary);
- outFile.write((char *)ws.c_str(), ws.length() * sizeof(wchar_t));
- outFile.close();
- }
- int main(int argc, _TCHAR* argv[])
- {
- wstring sText = readUnicodeFile("inputUnicode.txt");
- WriteUnicodetoFile("ouputUnicode.txt", sText);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement