-1

I wrote this simple code to understand how the argument system works. I dragged a textfile to the .exe file, and get 2 as output instead of 1 as i expected. Why 2? Is Arg 1 the .exe itself? How can i find out the filenames of the arguments?

#include<iostream>
using namespace std;

int main(int argc, char* argv[])
{
    int i = 0;
    const int max = 100000;

    if (argc > 0)
    {
        cout<< argc <<endl;
    }

    cin.get(); cin.get();

  return 0;
}

And an additional question. Where can i inform on how to access each argument and use the informations. My goal is to open all files passed as arguments to the .exe.


This is not a duplicate question, i asked why 2 is returned when you pass 1 argument. The question in the Link is another...

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Black
  • 18,150
  • 39
  • 158
  • 271
  • 3
    The program always receives at least 1 argument, which is the program's name. – aslg Jun 29 '15 at 09:28
  • 4
    Why didn't you output the values of `argv`? That would have answered your own question. – PaulMcKenzie Jun 29 '15 at 09:29
  • Because i did not knew that the values are stored here. As you can read in my question: "How can i find out the filenames of the arguments?" – Black Jun 29 '15 at 09:40
  • The argument may _be_ a filename, if that's what you pass in - but it's still just an argument. Print it like any other variable. You don't need to "find the filename" of a variable to see what's in there. – Useless Jun 29 '15 at 10:17
  • 1
    Why did this Question get so many down-votes? The only "fault" I see, is the question being very basic. But it explains what the inquirer has tried so far and even provides even a minimal example. – Sascha Jun 29 '15 at 10:23
  • i also don't understand the downvotes, there is not even feedback so i can learn what i can improve in my next question. Maybe some trolls are on this site. – Black Jun 29 '15 at 10:54
  • 2
    @EdwardBlack Maybe the "research effort" was not enough. If you know what `argc` is, but wasn't in the least bit curious as to what the `argv` sitting right next to it means, maybe those downvotes are warranted. – PaulMcKenzie Jun 29 '15 at 14:51
  • Lol, marked as duplicate even when i explained why it is no duplicate. – Black Jun 30 '15 at 13:12
  • The answer on the other question explains what `argc` and `argv` are, and that : "virtually all implementations will prepend the name of the program to the array". It is a duplicate because you can find answers to all your questions there, and there is no need for so many answers repeating the same thing. – Leiaz Jun 30 '15 at 13:24
  • 1
    thats like saying "Read this C++ Book, all is explained there" – Black Jun 30 '15 at 13:26

3 Answers3

7

argv[0] is normally the name of the program being run, and is counted in argc. If argc >= 2, the first of your text-file names should be in argv[1]. You can simply loop over them...

for (size_t i = 1; i < argc; ++i)
    if (std::ifstream infile(argv[i]))
        ...use infile...
    else
        std::cerr << "ERROR - unable to open " << argv[i] << '\n';

For more complex requirements, you may want to use getopt() et al if your system provides it, or the boost library equivalents.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
2

According to the C++ Standard (3.6.1 Main function)

  1. ...If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (ntmbs s) (17.5.2.1.4.2) and argv[0] shall be the pointer to the initial character of a ntmbs that represents the name used to invoke the program or "".

To output all arguments you can use various approaches as for example

for ( char **s = argv; *s; ++s ) std::cout << *s << std::endl;

for ( int i = 0; i < argc; i++ ) std::cout << argv[i] << std::endl;


#include <algorithm>
#include <iterator>
//...
std::copy( argv, argv + argc, std::ostream_iterator<char *>( std::cout, "\n" ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

first argument for main is always name of execution file(means .exe file name) that is why the value of argc is 2 ( 1 for program name and other for your .txt file) you can check it by printing argv[0]

suraj.tripathi
  • 417
  • 2
  • 15