Simple c++ exercise which run sed command from c++ program.
The program is explained at:
main page
- #include <iostream>
- #include <string>
- using namespace std;
- int main (int argc, char *argv[]) {
-
- string str("\"1223a2ee\"");
- string cmd("echo ");
- cmd = cmd + str + " | sed 's/\\(....\\)\\(....\\)/\\1_\\2/'";
- cout << cmd << endl;
- FILE* pfd = popen(cmd.c_str(), "r");
-
- if (pfd)
- {
- while (!feof(pfd))
- {
- char buf[1024] = {0};
-
- if (fgets(buf, sizeof(buf), pfd) > 0)
- {
- cout << "buf = " << buf; // a newline is already present
- }
- }
- pclose(pfd);
- }
-
- return 0;
- } //main
The output looks like:
- echo "1223a2ee" | sed 's/\(....\)\(....\)/\1_\2/'
- buf = 1223_a2ee
a better example
|