Reading command line arguments after '<' in C

Let me start by saying that this is related to homework. However, this is a very small and relatively minor part of the assignment.

Program C receives input through command line arguments, but it should be in the form:

 $ ./program < input 

How can I get this input as a string? Every time I try to print the third argument from argv, I get this message:

: There is no such file or directory.

+6
source share
10 answers

<this is a shell redirection - it is processed outside of your program. You will see that the contents of the file name "enter" are sent to standard input. This is the usual way that programs work, although they usually also process a file name, for example. sed .

If I had to guess, I would have thought:

 input: No such file or directory. 

comes from the shell because it cannot open the specified file: "input".

On the other hand, if you really want to use < input as arguments for your program, you can avoid or quote them so that the shell does not interpret them. (Exit as an exercise for the reader :-)).

+21
source

Syntax ./program < input is a special shell syntax that says: "Redirects everything to a file called input to the standard program entry."

To read the input, your program just needs to use the standard input reader functions, the fgets or scanf .

+10
source

On * nix systems there will be no third argv element. If you run this command for almost any Unix-like shell, it will look like this:

 cat input | ./program 

So your ./program has only one element in argv , but it stdin is an input file, so to read the file you would just read from stdin . Please note that this is the absolutely correct way to develop your program. Many Unix programs are read from standard input if no files are specified, so you can connect to the input from other programs (or in this case from files).

+5
source

What happens after < not a command line argument. The contents of the file will be transferred to your program by the shell.

All you have to do is read from stdin and you will get the contents of the file.

+3
source

You need to avoid the "<", otherwise the shell will parse it and the program will not receive it on the command line.

If you use bash, then:

 ./program '<' input 

or

 ./program \< input 

Other shells may do this differently (for example, Windows by default, cmd.exe , uses ^ as an escape character, not \ ).

+3
source

This is a Unix shell. The form someprogram < somefile tells someprogram to run using some file as its input. If you want to do something different with the < character, you need to quote it.

0
source

< means that the program will read standard input (stdin) from the named file (input). So just read stdin (using fgets , fread , etc.).

0
source

Leave the value '<'. You need command line arguments:

$. / program -Dflag seven = ixnay FromDinger

In your application, try the following:

 int main( int argc, char **argv ) { int i; for( i = 0 ; i < argc ; ++i ) printf( "Arg %d = %s\n", i, argv[i] ); return 0; } 

You will notice that the first argument is the name of the executable file (at index 0), and your second argument (at index 1) will be "-Dflag"

0
source

Actually, this is a very common method used in programming tournaments. The data needed by your program is stored in a file, say data.txt, and then redirected to your application using the "<" on the shell, for example: ./program <data.txt

So, in your source code, what you need to do is something like this:

 #include <iostream> #include <string> using namespace std; int main(void) { string tmp; string full_content; while (cin >> tmp) full_content += " "+tmp; cout << full_content << endl; } 

.. and you will get all the data from the file in a line (and separated by spaces).

This way to do it, hope this helps. [] 'S

0
source

You can get it by reading stdin.

-2
source

All Articles