Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * retrieve string response from a command , in linux
- */
- #include <stdio.h>
- #include <string>
- #include <iostream>
- #include <sys/syscall.h> /* Pour les définitions de SYS_xxx */
- using namespace std;
- string command="ps -aux | grep ttyUSB";
- //retrieve string response from a command
- string GetStdoutFromCommand(string cmd) {
- string data;
- FILE * stream;
- const int max_buffer = 256;
- char buffer[max_buffer];
- cmd.append(" 2>&1");
- stream = popen(cmd.c_str(), "r");
- if (stream) {
- while (!feof(stream))
- if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
- pclose(stream);
- }
- return data;
- }
- int main(int argc, char* argv[])
- {
- string tmp=GetStdoutFromCommand(command);
- cout <<tmp<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement