views:

506

answers:

10

Let me start by saying this is associated with a homework assignment. However, this is a very small and relatively insignificant part of the assignment.

The C program receives input via command line arguments but it needs to be in the form:

$ ./program < input

How, would I go about receiving that input as a string? Each time I try to print out the 3rd argument from argv I receive this message:

input: No such file or directory.

+20  A: 

< is a shell redirect - it is handled outside your program. What you'll see is the contents of the file name 'input' being send to your standard input stream. This is a common way for programs to operate, although they usually also handle being given a file name e.g. sed.

If I had to guess I would think the:

input: No such file or directory.

is coming from the shell, as it is unable to open the file specified: "input".

On the other hand, if you actually want the < input as arguments to your program, you can escape or quote them so the shell won't interpret them. (Escaping left as an exercise for the reader :-)).

Douglas Leeder
Wow, 6 votes in the first minute!
Michael Myers
That's quite a shot you have there, Mr. Eastwood.
Chris Lutz
+3  A: 

What comes after the < is not a command-line argument. The contents of the file will be piped into your program by the shell.

All you need to do is read from stdin and you'll get the contents of the file.

sepp2k
+5  A: 

On *nix systems, there won't be a third element of argv. If you execute that command on almost any Unix-like shell, it will be similar to doing this:

cat input | ./program

So your ./program has only one element in argv, but it's stdin is the file input, so to read the file you would just read from stdin. Note that this is a perfectly valid way to design your program. Many Unix programs read from standard input if no files are given, so that you may pipe in input from other programs (or in this case, from files).

Chris Lutz
Not only on *nix systems. DOS/Windows and OS/2 handle this the same way. And by the way, explaining '<' shell operator with '|' doesn't really clarify the issue ;-)
Frank Bollack
It may not make it immediately more obvious, but it does show more clearly that `./program` isn't being given any arguments.
Chris Lutz
This is actually NOT true for DOS -- in DOS the entire command line (with the redirect) is passed to the program and it is responsible for opening the file and such. Of course, if you use a C compiler, the compiler's startup routine which runs before main will do this for you...
Chris Dodd
+10  A: 

The ./program < input syntax is a special shell syntax saying "Redirects everything in the file named input to the standard entry of the program".

To read the input, your program just have to use standard input reading functions, line fgets or scanf.

Pierre Bourdon
*"To read the input, your program just have to use standard input reading functions, line `fgets` or `scanf`."* Specifically, on the `stdin` stream.
T.J. Crowder
@T.J. What other stream does `scanf()` operate on? :P
Chris Lutz
+3  A: 

You need to escape the '<', otherwise shell will parse it, and program won't receive it in command-line.

If you're using bash, then:

./program '<' input

or

./program \< input

Other shells might do it differently (e.g. Windows' default, cmd.exe, uses ^ as escape character, not \).

PiotrLegnica
A: 

You can get it by reading stdin.

Ashwin
A: 

This is a Unix shell thing. The form someprogram < somefile tells someprogram to run using somefile as its input. If you want to do something different involving the < symbol, you'll need to quote it.

Chuck
A: 

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

Dave Hinton
A: 

Leave off the '<'. You want command line arguments do this:

$ ./program -Dflag seven=ixnay FromDinger

In your application, try this:

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'll notice that the first argument is the name of the executable (at index 0), and your second argument (at index 1) will be "-Dflag"

Kieveli
A: 

Actually, this is a very common technique used in programming tournaments. The data your program needs is stored in a file, let's say data.txt , and then redirected to your application using the "<" on the shell, like this: ./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'll get all the data from the file on a string (and separated by spaces).

That's one way to do it, I hope it helps. []'s

karlphillip