{"id":5423,"date":"2014-03-30T21:50:51","date_gmt":"2014-03-30T21:50:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/trouble-getting-standard-output-in-java-from-a-c-program-with-getchar-collection-of-common-programming-errors\/"},"modified":"2014-03-30T21:50:51","modified_gmt":"2014-03-30T21:50:51","slug":"trouble-getting-standard-output-in-java-from-a-c-program-with-getchar-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/trouble-getting-standard-output-in-java-from-a-c-program-with-getchar-collection-of-common-programming-errors\/","title":{"rendered":"Trouble getting standard output in Java from a C program with getchar()-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to call a C program from Java and capture the standard output. Here is my code:<\/p>\n<pre><code>try {\n    ProcessBuilder pb = new ProcessBuilder(\"helloworld.exe\");\n    pb.redirectErrorStream(true);   \/\/ Merge std out and std err into same stream\n    program = pb.start();           \/\/ Start program\n\n    BufferedReader input = new BufferedReader(new InputStreamReader(program.getInputStream()));\n\n    line = input.readLine();\n    while (line != null) {\n        System.out.println(line);\n        line = input.readLine();\n    }   \n} catch (IOException e) {\n    e.printStackTrace();\n}\n<\/code><\/pre>\n<p>Here is a sample c program:<\/p>\n<pre><code>int main(){\n    printf(\"Hello world\\n\");\n}\n<\/code><\/pre>\n<p>This works fine when the program I&#8217;m executing (helloworld in this case) does not have a <code>getchar()<\/code> in it. However, if I add a <code>getchar()<\/code> right after the printf, I never get anything off the input stream. Any ideas why?<\/p>\n<p>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to call a C program from Java and capture the standard output. Here is my code: try { ProcessBuilder pb = new ProcessBuilder(&#8220;helloworld.exe&#8221;); pb.redirectErrorStream(true); \/\/ Merge std out and std err into same stream program = pb.start(); \/\/ Start program BufferedReader input = new BufferedReader(new InputStreamReader(program.getInputStream())); line = input.readLine(); while (line != null) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5423","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5423"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5423\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}