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?

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top