Question

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.

Was it helpful?

Solution

< 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 :-)).

OTHER TIPS

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.

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).

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.

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 \).

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.

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).

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"

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

You can get it by reading stdin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top