skip to Main Content

I have a file called HelloWorld.c that performs some operations to read standard input and create a jagged array of doubles. the problem is the reading from standard input. the code is working fine (the array is created) when I use this command:

./HelloWorld < sample.txt

but not with this one:

cat sample.txt | HelloWorld

The array is simply not created (0 elements) as if the content of sample.txt are never outputted to the c file
I need to be able to compile the code using the second command

Does anyone have an idea one what might be the issue, the code seems to be working fine since it compiles properly with one command…
(using linux system on windows, ubuntu)

2

Answers


  1. How are you reading the input in your source code? It may be as simple as using the incorrect stream type. With out more information it’s hard to say. For example getch input is from sdtout where as gets input is from stdin. See https://www.tutorialspoint.com/cprogramming/c_input_output.htm for more information Also found that from a google search for read input from output of command c

    Login or Signup to reply.
  2. The main difference between the two situations is that under the file redirection, your program’s standard input is a file descriptor, whereas in the cat case, it is a pipe.

    Some kinds of operations don’t work on pipes, like repositioning ("seeking"). If your program tries to rewind(stdin) or fseek(stdin, ...) to go back and re-read something, or skip forward, it won’t work on the pipe.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search