I am starting my c++ program from command line:
program input_file1 input_file2 output_file
where
int main( int argc, char *argv[] )
{
short indicator= 3;
char input_file1[4096], input_file2[4096], output_file[4096];
char *p_file = NULL;
while ( --argc > 0 ) {
switch (--indicator) {
case 2:
p_file = output_file;
break;
case 1:
p_file = input_file2;
break;
case 0:
p_file = input_file1;
break;
}
for (char *argument = argv[argc]; ; ++argument) {
if (*argument == '\0')
break;
else
*p_file++ = *argument;
}
*p_file = '\0';
}
std::cout << input_file1 << '\n';
std::cout << input_file2 << '\n';
std::cout << output_file << '\n';
}
But with the real arguments
program D:\\data\\file1.txt D:\\data\\file2.txt D:\\data\\file3.txt
in names of the files only the first letter D is stored...
Output:
D
D
D
Thanks for your help...
This is a C problem, not a C++ one, but as it is tagged C++, i will suggest a C++ solution for your problem :
UPDATE using iterators on argv to fill the vector args (thanks Space_C0wb0y)
Rather than copying the arguments, just set the file names to point to the appropriate entry in argv.
the contents of argv will exist for as long as the program is running.
This assigns the first character of arguement to the first character in p_file.
You need to use
strcpy
, or declare somestd::strings
instead of arrays of charYour loop is all wrong. You are looping through characters, not the parameter array. You can do that like this:
This works because the arguments (if any) start at
argv + 1
and because they are null terminated so that*arg
isnullptr
(converts tofalse
) at the end of the argument array.Your arguments are obtained by dereferencing
arg
using*arg
which is achar*
.So you can do:
Ok, so here is the short version:
You should be able to take it from here.