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:
imageshouldbevalid

but when create file via cmd (example command below), , run program, receive unexpected result:
imageshouldbevalid cmd /c "hostname">"c:\users\admin\desktop\lel.txt" & exit result:
imageshouldbevalid

when open file generated cmd (lel.txt), file contents:
imageshouldbevalid

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

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -