mardi 2 juin 2015

Reading file made by cmd, results in 3 weird symbols

Im using this piece of code to read a file to a string, and its working perfectly with 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 I create a file via notepad (or any other editor) and save it to something, I get this result in my program:
ImageShouldBeValid

But when I create a file via CMD (example command below), and run my program, I receive an unexpected result:
ImageShouldBeValid cmd /C "hostname">"C:\Users\Admin\Desktop\lel.txt" & exit Result:
ImageShouldBeValid

When I open this file generated by CMD (lel.txt), this is the file contents:
ImageShouldBeValid

If I edit the generated file (lel.txt) with notepad (adding a space to the end of the file), and try running my program again, I get the same weird 3char result.

What might cause this? How can I read a file made via cmd, correctly?

Aucun commentaire:

Enregistrer un commentaire