i facing issues non-ascii chars in c++. have 1 file containg non-ascii chars reading in c++ via file handling. after reading file(say 1.txt) storing data string stream , writing file(say 2.txt).
assume 1.txt contains:
ação
in 2.txt should same ouyput non-ascii chars printed hex value in 2.txt.
also, quite sure c++ handling ascii chars ascii only.
please on how print these chars correctly in 2.txt
edit:
firstly psuedo-code whole process:
1.shell script read db 1 value , stores in 11.txt 2.cpp code(a.cpp) reading 11.txt , writing f.txt
data present in db being read: instalação
file 11.txt contains: instalação
file f.txt contains: instalação
ouput of a.cpp on screen: instalação
a.cpp
#include <iterator> #include <iostream> #include <algorithm> #include <sstream> #include<fstream> #include <iomanip> using namespace std; int main() { ifstream myreadfile; ofstream f2; myreadfile.open("11.txt"); f2.open("f2.txt"); string output; if (myreadfile.is_open()) { while (!myreadfile.eof()) { myreadfile >> output; //cout<<output; cout<<"\n"; std::stringstream tempdummylineitem; tempdummylineitem <<output; cout<<tempdummylineitem.str(); f2<<tempdummylineitem.str(); } } myreadfile.close(); return 0; }
locale says this:
lang=en_us.utf-8 lc_ctype="en_us.utf-8" lc_numeric="en_us.utf-8" lc_time="en_us.utf-8" lc_collate="en_us.utf-8" lc_monetary="en_us.utf-8" lc_messages="en_us.utf-8" lc_paper="en_us.utf-8" lc_name="en_us.utf-8" lc_address="en_us.utf-8" lc_telephone="en_us.utf-8" lc_measurement="en_us.utf-8" lc_identification="en_us.utf-8" lc_all=
at least if understand you're after, i'd this:
#include <iterator> #include <iostream> #include <algorithm> #include <sstream> #include <iomanip> std::string to_hex(char ch) { std::ostringstream b; b << "\\x" << std::setfill('0') << std::setw(2) << std::setprecision(2) << std::hex << static_cast<unsigned int>(ch & 0xff); return b.str(); } int main(){ // test purposes, we'll use stringstream input std::stringstream infile("normal stuff. weird stuff:\x01\xee:back normal"); infile << std::noskipws; // copy input output, converting non-ascii hex: std::transform(std::istream_iterator<char>(infile), std::istream_iterator<char>(), std::ostream_iterator<std::string>(std::cout), [](char ch) { return (ch >= ' ') && (ch < 127) ? std::string(1, ch) : to_hex(ch); }); }
Comments
Post a Comment