我需要逐行将bash命令的输出读入字符串向量中。我使用了ifstream来尝试这个代码,但出现了错误。我应该使用什么来代替ifstream解析它们?
using namespace std;
int main()
{
vector<string> text_file;
string cmd = "ls";
FILE* stream=popen(cmd.c_str(), "r");
ifstream ifs( stream );
string temp;
while(getline(ifs, temp))
text_file.push_back(temp);
for (int i=0; i<text_file.size(); i++)
cout<<text_file[i]<<endl;
}
ifstream
与C库文件流一起使用。您需要坚持使用C I/O,并可能将其结果转换为std::string
。您**真正想要的是Boost.Filesystem。 - pmrls
实际上是他想要实现的,但我可能错了。抱歉。 - pmr