0

I'm trying to run this C source block using input from a text file.

#+BEGIN_SRC C :includes <stdio.h>
  int c;
  c = getchar();
  while (c != EOF) {
    putchar(c);
    c = getchar();
  }
#+END_SRC

How can I handle "./example.txt" as my input?

1 Answer 1

1

On Linux at least (and probably other Unix systems and probably also on WSL), you can use input redirection as follows:

#+BEGIN_SRC C :includes <stdio.h> :cmdline < ./example.txt
  int c;
  c = getchar();
  while (c != EOF) {
    putchar(c);
    c = getchar();
  }
#+END_SRC

The :cmdline value is appended to the path of the executable and the whole thing is sent to a shell for evaluation, so redirection works.

You can find some information on the C implementation in Org Babel on Worg. However, :cmdline is only mentioned there with no examples of how to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.