20

Is their any C popen() equivalent in C++ ?

Arif
  • 978
  • 12
  • 29
  • 2
    Notice that `popen` is *not* a function prescribed by the *[C99](https://en.wikipedia.org/wiki/C99) standard*. It is in POSIX, so it is available in your C++ code for POSIX systems. – Basile Starynkevitch Sep 20 '14 at 07:17

3 Answers3

14

You can use the "not yet official" boost.process if you want an object-oriented approach for managing the subprocess.

Or you can just use popen itself, if you don't mind the C-ness of it all.

Stephen
  • 47,994
  • 7
  • 61
  • 70
9

There is no C++ equivalent in any Standard, however C++ wrappers around this function (and other POSIX process function) can be found in various UI Toolkit (e.g QT, glibmm) and in the pstreams library.

6

C++ is a superset of C. popen is available to C++ code as well.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 1
    I don't think that popen is required by the Standard. – Puppy Jul 06 '10 at 21:55
  • 2
    Which standard? popen comes from POSIX.1-2001. Even Windows has a POSIX-compatibility layer. Linux and MacOS support it natively, of course. It's a library function, not part of the C standard itself. – Borealid Jul 06 '10 at 21:58
  • popen() is part of POSIX, so any UNIX-like operating system should support it. Even for non-POSIX OSes, if you have popen() in C, you should be able to use it in C++, unless there's something very wrong with your compiler. – Chris Jul 06 '10 at 21:59
  • 17
    Technically, C++ isn't a superset of C. – You Jul 06 '10 at 22:03
  • I know popen is available in c++. But I am flexible C++ iostream. – Arif Jul 06 '10 at 22:26
  • 9
    @You: Saying C++ isn't a superset of C is like saying Python 3.0 isn't a superset of Python 2.7. While technically true, it's not useful to anyone but pedantics trying to start an argument – BlueRaja - Danny Pflughoeft Jul 06 '10 at 22:37
  • 1
    @Danny: But it is true. And the minimal effort required to write "C++ is, *for all intents and purposes*, a superset of C" is totally worth it. – You Jul 06 '10 at 22:43
  • @BlueRaja: I agree that in most cases it can be considered a superset, but there are some things that are supported in C and not in C++, like arrays whose size is defined at runtime: `void f( int x ) { int a[x]; }` And there are subtleties, like `void f(); void f( int i ) {}` in C defines a single function that takes an argument, in C++ it declares a function with no arguments and an overload with one argument... – David Rodríguez - dribeas Jul 06 '10 at 22:46
  • @David: Arrays whose size is defined at runtime is not an exactly well supported item in C. – Billy ONeal Jul 06 '10 at 23:51
  • @Billy: Visual Studio is the only compiler I know of that has no C99 support --and probably never will. The rest of the compilers I know (gcc, intel, sun, comeau) all have support for variable size arrays. Whether a single vendor make it *not exactly well supported* is debatable, kind of *POSIX is not a well supported standard*, as there are some OS that don't comply with it. – David Rodríguez - dribeas Jul 07 '10 at 07:36
  • 11
    C++ is absolutely *not* a superset of C. – Lightness Races in Orbit Jan 17 '11 at 14:39
  • @Blue what? "Python 3.0 isn't a superset of Python 2.7" <- that's true and it's a useful statement to do. I don't get your point. I doubt that python 3 differs by the degree C++ differs from C though. Much features of C aren't present in C++ and vice versa. – Johannes Schaub - litb Jan 17 '11 at 16:42