4

This is the continuation of my previous question, In C++, how to read the contents of a text file, and put it in another text file?

In that, I was able to able to open an input file input.txt and read it contents successfully, but now i don't want to hardcode or give the input filename beforehand,

ifstream myfile ("input.txt");
if (myfile.is_open())

but i want to give the input file name later after compiling the program and generating an executable file named testin the command line, as shown below

./test input.txt

Any suggestions on how to do this ?

Community
  • 1
  • 1
user2917559
  • 163
  • 1
  • 2
  • 12

4 Answers4

4

You can access the command line arguments passed to your program in the main function:

int main(int argc, char *argv[]) { }

argc is the number of arguments passed to your program and argv contains pointers to C-strings holding the arguments passed to your program. So using this array you can access the arguments passed to your program.

But you have to pay attention: the program itself is always passed to the program as first argument. So argc is always at least one and argv[0] contains the program name.

If you want to access the input.txt from your post you could write:

int main(int argc, char *argv[]) {
   if (argc > 1) {
      // This will print the first argument passed to your program
      std::cout << argv[1] << std::endl;
   }
}
sleepy1771
  • 313
  • 1
  • 8
  • 1
    Also, `std::vector args(argv, argv + argc);` is a convenient way to access arguments. – Dan Feb 14 '14 at 21:46
  • And if you use `argv+1` instead of `argv` you omit the program name if you don't need it. @Dan That's really a very nice way to access the arguments... – sleepy1771 Feb 14 '14 at 21:50
3

Just to addon to all the answers - You can use 'standard input and output' of C/C++ like printf() & scanf()

$ ./a.out < input.file // input file
$ ./a.out > output.file // output file
$ ./a.out < input.file > output.file // merging above two commands.

For more info: REFER THIS

And ofcourse, clean way is to use argc/argv as answered by fellow gentlemen.

Ankit Singh
  • 2,602
  • 6
  • 32
  • 44
  • Thanks, what is 'a.out' here ? is it the executable file generated after compiling, or is it something else ? – user2917559 Feb 14 '14 at 21:58
  • Yes, when you compile your C++ in Unix environment like 'g++ sample.cpp' will output binary executable file with default name a.out else you can specify name of your output file as 'sample_exec' like 'g++ sample.cpp -o sample_exec'. Then similarly you can run './sample_exec < file.input' as described above – Ankit Singh Feb 14 '14 at 22:05
  • Ok, thanks, i am working on windows and it is generating .exe file as the executable file – user2917559 Feb 14 '14 at 22:12
  • Doesn't matter, redirection also works in Windows but not sure. This link tells something like that (https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/filters.mspx?mfr=true). So, in principle you can do 'sample.exe < input.file'. Give it a shot and let me know as I never had the chance try this on windows – Ankit Singh Feb 14 '14 at 22:17
0

That's what argv and argc are for.

int main(int argc, char **argv)
{
    assert(argc >= 2); // Not the best way to check for usage errors!
    ifstream myfile(argv[1]);
    …
}
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

argc and argv arguments to main would contain all input arguments to the program, google by argc/argv, or take a look at answers here

Community
  • 1
  • 1
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41