Reputation: 10204
I am testing a program "myprog.c" that crashes if it runs with any input parameter:
#include <stdlib.h>
int main(int argc, char * arg[]){
if (argc > 1 ){
abort();
}
}
As expected, "./myprog.out abc" crashes. But then I tried to get inputs from a file: "./myprog.out < inputs.txt", where inputs.txt has a couple of words, the program does not crash. Why not?
Upvotes: 0
Views: 139
Reputation: 1063
It's because argc is equal to 1, you can verifie it with the following code:
int main(int argc, char * arg[])
{
printf("argc = %i\n", argc);
if (argc > 1 ) {
abort();
}
}
output:
argc = 1
it appear because you can't pass argument like it, if you do it with a < your program will interpret it like it provide from stdin (filedescriptor numero 0)
if you want to pass more argument than 1, do like it:
./a.out abc def ghi
if you want get "argument" by a file, use a getline
Upvotes: 1
Reputation: 409176
That's because the shell doesn't pass < inputs.txt
as arguments. Instead the shell makes it so that the contents of inputs.txt
is to be read from stdin
.
Upvotes: 2