Search code examples
c++windowsnetstat

How to retrieve the result of netstat command


I tried to get the list of opened ports in my PC in a c++ code.So, I want to use the DOS command netstat. I have written this line system("netstat -a") but I can't retrieve the result that it returns.


Solution

  • You can start with this code

    int main() {
      char buf[10000];  
      FILE *p = _popen("netstat -a", "r");
      std::string s;
      for (size_t count; (count = fread(buf, 1, sizeof(buf), p));)
          s += string(buf, buf + count);
      cout<<s<<endl;    
      _pclose(p);  
    }