Like I said, do not write to argv. It is already filled with whatever arguments were provided when the program was executed (with argv[0] being the command used to invoke the program).
You can see how many arguments were provided by looking at the value of
argc
. This will always be at least 1 (since argv[0] will be the first argument).
1 2 3 4
|
for (int i = 0; i < argc; i++)
{
std::cout << "Argument #" << i << ": " << argv[i] << std::endl;
}
|
If you need spaces in the file names, I recommend not trying to provide those in command line arguments, as those are delimited by whitespace characters.
Is there a particular reason why you need them as command line arguments? If the user is to manually enter them after the program has started, they won't be command line arguments.
If you need to get input from the user, store it in a string, like
std::string
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <string>
#include <iostream>
int main(int argc, char **argv)
{
for (int i = 0; i < argc; i++)
{
std::cout << "Argument #" << i << ": " << argv[i] << std::endl;
}
std::string input_file, output_file;
std::cout << "Enter name of input file: ";
std::getline(std::cin, input_file);
std::cout << "Reading from file: \"" << input_file << "\"" << std::endl;
std::cout << "Enter name of output file: ";
std::getline(std::cin, output_file);
std::cout << "Writing to file: " << output_file << "\"" << std::endl;
}
|
Produced this:
Argument #0: C:<some_arbitrary_path>CPPHelp.exe
Enter name of input file: File to read from
Reading from file: "File to read from"
Enter name of output file: File to write to
Writing to file: File to write to"
Press any key to continue . . . |
If you must retrieve the information from the command line, you can just read it by referencing
argv[i]
, but do not write to it. You can pass it as a parameter to the string constructor if you wish to put it into a string
std::string str(argv[i])
just do not do that unless you know that
argv
actually contains that (like do not read argv[3] if only 3 arguments were provided, since you'd need 4 arguments to have argv[3]).
There is no reason to use
printf
or
scanf
from what I can see, just use
std::cout
and
std::cin
, respectively (unless, of course, performance is absolutely critical).
I also don't know why you're using
system("PAUSE")
, but you probably shouldn't. There are plenty of other ways to make your program wait for something.