Using file from command line

I'm trying to read in a file specified in the command line but I'm having some trouble. The command line entry specifies the inputfile preceded by '<' and the output file preceded by '>' like so

./program -v < input_file.cmd > output_file.cmd

This is what I've got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main(int argc,char* argv[])
{


string strv="-v";
string input="<";
string output=">";
string str;
string input_file;
const char* in=input_file.c_str();
string output_file;
const char* out=output_file.c_str();
bool verbose=false;
int i;
//deal with command line paramaters
for (i=0;i<argc;i++)
{
cout<<"argc="<<argc<<" and argv at "<<i<<" is "<<argv[i]<<endl;
	if (argv[i]==strv)
	verbose=true;
	else if (argv[i]==input)
	input_file=argv[i+1];
	else if (argv[i]==output)
	output_file=argv[i+1];
	else	// nop
	;
}


ifstream fin(in,ifstream::in);	


This compiles ok, but when i run it using
./program -v < test1.cmd
I get a segfault, if i cout argc it returns '2' where i would expect for this command line entry, I 'd get 4.

I'm not yet outputting to file, just to the screen so im not specifying an output file yet.
If anyone could help on this it'd be much appreciated.
The value of variable in is an address of an empty character array that (the address) becames invalid after assigning a new value to input_file. You should remove declarations of in and out and use

ifstream fin(input_file.c_str() ifstream::in);

instead of

ifstream fin(in,ifstream::in);
Last edited on
Thanks for that, however argc is still taking a value of 2 when there should be 4??
I've found now that if i change string input="in"
and pass
./program -v in test1.cmd
to the command line it works fine however the spec for my assignment requires the < before the filename?
I'm using cygwin so im not sure if my issue is specific to that or if < has some special meaning as this and anything beyond it is not being read into argv[].
Symbols < and > has special meaning in the command line. Symbol < means that the standard input stream will be read from a file with the name following <. Symbol > means that standard output stream will be redirected in a file with the name following >. So the command line processor processes this symbol before passing the command line parameters to a calling program.
Last edited on
ok, things are starting to make sense now, so what parameter do i enter when i create my input stream?

ifstream fin(????????,ifstream::in);
Use the command line without symbols < and >

program -v input_file.cmd output_file.cmd
The assignment spec requires the use of the symbols, my code will be run automatically by the lecturers marking script.
I am not sure bur try to use them without spaces between them and the filename.
Thanks for your help people, I've got it sorted now.

Turns out i don't need to create a filestream, i just use getline(cin,string) and it reads from the specified file.
Last edited on
Topic archived. No new replies allowed.