{"id":1418,"date":"2015-06-20T00:01:42","date_gmt":"2015-06-20T07:01:42","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1418"},"modified":"2022-01-03T07:23:39","modified_gmt":"2022-01-03T15:23:39","slug":"the-marvelous-popen-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1418","title":{"rendered":"The Marvelous <em>popen()<\/em> Function"},"content":{"rendered":"<p>To launch and run another program from within your code, use the <em>system()<\/em> function. When your code must examine or save that program&#8217;s output, use the <em>popen()<\/em> function.<br \/>\n<!--more--><br \/>\nLike <em>system()<\/em>, <em>popen()<\/em> starts a second program or process, which is where the <em>p<\/em> comes from.<\/p>\n<p>The <em>open<\/em> part from <em>popen()<\/em> is the same <em>open<\/em> found in the <em>fopen()<\/em> function. Similar to that function, <em>popen()<\/em> (say &#8220;pee-open&#8221;) opens a process for input or output. The <em>popen()<\/em> function uses a program name as its first argument. The second argument is a file mode, such as <code>\"r\"<\/code> to read, <code>\"w\"<\/code> to write, or <code>\"r+\"<\/code> for both.<\/p>\n<p>Once opened, the same <em>FILE<\/em> type pointer is used as a reference.<\/p>\n<p>When open for input (as shown below), the spawned program&#8217;s output is captured. Use the standard file-reading functions to process the second program&#8217;s output.<\/p>\n<p>When the spawned process ends, or your code has read enough, use the <em>pclose()<\/em> function to terminate the stream.<\/p>\n<p>Both <em>popen()<\/em> and <em>pclose()<\/em> require inclusion of the <code>stdio.h<\/code> header file.<\/p>\n<p>In the following code, the <em>popen()<\/em> function launches the <em>ver<\/em> or <em>uname<\/em> command. <em>ver<\/em> is the DOS command to display the operating system version; <em>uname<\/em> is the corresponding Unix command. Ensure that you make the proper change in the code to set the right <em>popen()<\/em> statement. <\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    FILE *p;\r\n    int ch;\r\n\r\n    p = popen(\"ver\",\"r\");   <span class=\"comments\">\/* DOS *\/<\/span>\r\n<span class=\"comments\">\/*  p = popen(\"uname\",\"r\"); \/* Unix *\/<\/span>\r\n    if( p == NULL)\r\n    {\r\n        puts(\"Unable to open process\");\r\n        return(1);\r\n    }\r\n    while( (ch=fgetc(p)) != EOF)\r\n        putchar(ch);\r\n    pclose(p);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Microsoft Windows [Version 6.1.7601]<\/code><\/p>\n<p>I&#8217;ve used <em>popen()<\/em> in my code to read and store output from another program. Specifically, <em>popen()<\/em> spawned a utility and my code used that utility&#8217;s output to help process some data. Storing input from another program works just like storing input from a file. So it may sound crazy, but it&#8217;s not.<\/p>\n<p>The <em>popen()<\/em> function can also write to a program, as well as both read and write, but that&#8217;s where things get tricky.<\/p>\n<p>C language stream I\/O is buffered. If you plan on writing an interactive program, then you need to know how to manage buffered I\/O so that your code doesn&#8217;t sit and wait. Such a programming challenge could be highly frustrating. For processing input only, however, <em>popen()<\/em> is a great function for fetching data generated by another program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Run another program and steal its output. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1418\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1418","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1418"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1418\/revisions"}],"predecessor-version":[{"id":5156,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1418\/revisions\/5156"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}