0

I know that when I write :

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

  return 0;
}

I could get command line inputs such as files to read input from it. However, what should I do when the input file will be given after compiled. I mean assume that the name of input file inputFile.txt and my code is stored in main.cpp. I'll make following.

g++ main.cpp -o main
./main inputFile.txt

To be able to get inputFile.txt, what should I do?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
caesar
  • 2,865
  • 11
  • 29
  • 36
  • Get file name from args, check to see if file exists, open file, get contents, profit – afuzzyllama Nov 26 '13 at 18:29
  • @afuzzyllama main(int argc,char *argv[] ) is enough to get input file in this case ? – caesar Nov 26 '13 at 18:30
  • 1
    This might help : http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – afuzzyllama Nov 26 '13 at 18:31
  • `char *pszFileName = argv[0]` (or perhaps `argv[1]` in some cases). On you to handle if not enough args (by checking `argc`). –  Nov 26 '13 at 18:32
  • @afuzzyllama thanks for helps,I've done what I wanted.For separate question, now I stored my file name into argv[1] I guess. What I need to do is reading its content.To do that what should I do ? – caesar Nov 26 '13 at 18:49
  • 1
    @caesar ask another question. (and when you do check for duplicates) ie: reading C++ file (known filename) –  Nov 26 '13 at 19:00

3 Answers3

4

argc stores the number of arguments. argv is an array of arguments.

argv[0] will be the program name, if available. See: Is "argv[0] = name-of-executable" an accepted standard or just a common convention?

If you are passing only one argument, you can then use argv[1], which is your input file name, to do whatever operations you need.

Please see What does int argc, char *argv[] mean? for more information.

Community
  • 1
  • 1
bblincoe
  • 2,393
  • 2
  • 20
  • 34
  • 1
    Trying to remember when argv[0] is the first actual parameter and when argv[1] is... –  Nov 26 '13 at 18:31
  • 2
    `argv[0]` is the program name. `argv[1]` is the first argument. – Mike Seymour Nov 26 '13 at 18:31
  • @MikeSeymour except when it isn't :-) Which, could be another language by the way... I float around double time... –  Nov 26 '13 at 18:33
  • Only if the name is available. See: http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi – bblincoe Nov 26 '13 at 18:33
  • No, `argv[0]` is always the program name if `argc > 0`. If the program doesn't have a name, then `argv[0][0] == '\0'` (i.e.`argv[0]` is an empty string). But `argv[0]` is never an argument passed to the program. `argv[1]` is always the first argument passed to the program. – Cornstalks Nov 26 '13 at 18:38
0

You have two choices.

In the case you entered, the string "inputFile.txt" will be in argv[1] (arv[0] is your actual command). You can then use your operating systems to read from this file.

What may be simpler is to use redirection; i.e. change your command line to "./main < inputFile.txt" Then, the contents of that file will be in the input stream.

The first method (or writing your program to support both) would be preferred for commercial applications, but the second will do for learning purposes.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49
0

in pseudocode:

foreach(argv) {  
    if(argv exists)  
    open filehandle;   
    do stuff  
} else {  
    printf "Your file does not seem to exist";  
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Isaac
  • 625
  • 1
  • 12
  • 30