Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

Simple c++ exercise which run sed command from c++ program. The program is explained at: main page

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main (int argc, char *argv[]) {
  5.   
  6.   string str("\"1223a2ee\"");
  7.   string cmd("echo ");
  8.   cmd = cmd + str + " | sed 's/\\(....\\)\\(....\\)/\\1_\\2/'";
  9.   cout << cmd << endl;
  10.   FILE* pfd = popen(cmd.c_str(), "r");
  11.   
  12.   if (pfd)
  13.   {
  14.     while (!feof(pfd))
  15.     {
  16.       char buf[1024] = {0};
  17.       
  18.       if (fgets(buf, sizeof(buf), pfd) > 0)
  19.       {
  20.         cout << "buf = " << buf; // a newline is already present
  21.       }
  22.     }
  23.     pclose(pfd);
  24.   }
  25.   
  26.   return 0;
  27. } //main

The output looks like:
  1. echo "1223a2ee" | sed 's/\(....\)\(....\)/\1_\2/'
  2. buf = 1223_a2ee

a better example

  ...


Search This Site


Feedback This Site





new pages on this site