Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main's char *argv[] doesn't include arguments if stdin is file

I'm a little newbie in C.

./main 7 4

This command in terminal causes that argv[1] = 7, argv[2] = 4.

./main <input.in

When I use redirecting of stdin in terminal, it only shows argc = 1(./main).

Is there any way how to solve this problem or do i have to try a different way?

like image 554
Vospala Avatar asked Nov 20 '25 08:11

Vospala


1 Answers

You shell interprets the <file.txt as "send file.txt's contents to this processes' stdin", so your program will never see the <file.txt part.

There is no way to resolve this, as this is a deliberate feature of POSIX (and possibly other) shells.

like image 178
kdmurrray91 Avatar answered Nov 21 '25 22:11

kdmurrray91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!