Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

It sounds like you could use std::ifstream --> http://www.cplusplus.com/doc/tutorial/files/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top