I am currently trying to output the result of a process. This is the code I am using. This code runs a cmd command and outputs the content of the screen to a file.
I have a buffer of 128. However due to certain reasons I only want to write to a file once the captured data is at least 1200. (it actually does more than just write to a file but I have omitted that part to simplify the code). It seems like this logic is flawed as the output of the file is not exactly like the output of the screen if the command was run on a terminal. I would like some help in figuring where that issue is.
std::array<char, 128> buffer;
const int AtLeastlimit = 1200;
std::string partial;
std::ofstream outputFile;
outputFile.open(filename);
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), (int)buffer.size(), pipe.get()) != nullptr)
{
partial += buffer.data();
if (partial.length() >= AtLeastlimit)
{
outputFile << partial;
partial = "";
}
}
if (partial != "") {
outputFile << partial;
}
outputFile.close();
Aucun commentaire:
Enregistrer un commentaire