c++ - Reading file made by cmd, results in 3 weird symbols -
im using piece of code read file string, , working files manually made in notepad, notepad++ or other text editors:
std::string utils::readfile(std::string file) { std::ifstream t(file); std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>()); return str; }
when create file via notepad (or other editor) , save something, result in program:
but when create file via cmd (example command below), , run program, receive unexpected result:
cmd /c "hostname">"c:\users\admin\desktop\lel.txt" & exit
result:
when open file generated cmd (lel.txt), file contents:
if edit generated file (lel.txt) notepad (adding space end of file), , try running program again, same weird 3char result.
what might cause this? how can read file made via cmd, correctly?
edit
changed command (now using powershell), , added function found, named skipbom, , works:
powershell -command "hostname | out-file "c:\users\admin\desktop\lel.txt" -encoding "utf8""
skipbom:
void skipbom(std::ifstream &in) { char test[3] = { 0 }; in.read(test, 3); if ((unsigned char)test[0] == 0xef && (unsigned char)test[1] == 0xbb && (unsigned char)test[2] == 0xbf) { return; } in.seekg(0); }
this bom (byte order mark) : see here, means file saved in unicode bom. there way use c++ streams read files bom (you have use converters) - let me know if need that.
Comments
Post a Comment