I'm trying to read Alt key symbols from one Unicode UTF-8 file, and write to another.
Input file looks like this>
ỊịỌọỤụṄṅ
Output file looks like this>
239 187 191 225 187 138 225 187 139 225 187 140 225 187 141 225 187 164 225 187 165 225 185 132 225 185 133 ('\n' after each 3 digit combination, instead of ' ')
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <Windows.h>
///convert as ANSI - display as Unicode
std::wstring test1(const char* filenamein)
{
std::wifstream fs(filenamein);
if(!fs.good())
{
std::cout << "cannot open input file [" << filenamein << "]\n" << std::endl;
return NULL;
}
wchar_t c;
std::wstring s;
while(fs.get(c))
{
s.push_back(c);
std::cout << '.' << std::flush;
}
return s;
}
int printToFile(const char* filenameout, std::wstring line)
{
std::wofstream fs;
fs.open(filenameout);
if(!fs.is_open())
return -1;
for(unsigned i = 0; i < line.length(); i++)
{
if(line[i] <= 126) //if its standard letter just print to file
fs << (char)line[i];
else //otherwise do this.
{
std::wstring write = L"";
std::wostringstream ss;
ss << (int)line[i];
write = ss.str();
fs << write;
fs << std::endl;
}
}
fs << std::endl;
//2nd test, also fails
char const *special_character[] = { "\u2780", "\u2781", "\u2782",
"\u2783", "\u2784", "\u2785", "\u2786", "\u2787", "\u2788", "\u2789" };
//prints out four '?'
fs << special_character[0] << std::endl;
fs << special_character[1] << std::endl;
fs << special_character[2] << std::endl;
fs << special_character[3] << std::endl;
fs.close();
return 1;
}
int main(int argc, char* argv[])
{
std::wstring line = test1(argv[1]);
if(printToFile(argv[2], line) == 1)
std::cout << "Writing success!" << std::endl;
else std::cout << "Writing failed!" << std::endl;
return 0;
}
What I was expecting was something similar to the values in this table: