Reputation:
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.
Upvotes: 7
Views: 1069
Reputation: 93410
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
Upvotes: 0
Reputation: 11075
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"
Upvotes: 0
Reputation: 53310
< 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 :-)).
Upvotes: 21
Reputation: 47052
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).
Upvotes: 0
Reputation: 237030
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.
Upvotes: 0
Reputation: 129774
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 \
).
Upvotes: 3
Reputation: 10880
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
.
Upvotes: 11
Reputation: 75389
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).
Upvotes: 5
Reputation: 370122
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.
Upvotes: 3