Search code examples
c++charpopenstdstring

C++ Read Pipe into String


I am writing a c++ program that executes shell commands. I am using the popen() function to run the commands. I would like my exec function to return std::string but I do not know how to read from a FILE into a string. Is there a way to do this without reading into a char[] first and then converting that into a string? If there are many which is the most efficient?


Solution

  • It is not possible. The popen() function returns a pointer to a FILE. You cannot read from a FILE directly into std::string. You can however convert char* to std::string which appears to be the best solution in your case.