Why do you need it? What is it?

I am learning from the book "Krupnik A. Tutorial C ++. Peter 2005" there in the topic "Array of Pointers" there is such a program:

#include < iostream> #include < windows.h> #include < fstream> using namespace std; int main(int argc, char *argv[]) { char s[255]; if (argc < 2) { CharToOem("arhc_gv.exe <файл>", s); cout << s << endl; return 1; } ifstream infile(argv[1]); if (infile.fail()) { CharToOem("ошибка при открытии файла", s); cout << s << endl; return 3; } infile.close(); } 

This is the question, what does this program do in essence, and why are these variables argc and argv?

  • four
    I recommend that you first learn the C language, according to the book by Kernighan and Ritchie "C Programming Language", and only then proceed to the pluses. - Fiztex
  • 3
    Many say that "C" to "C ++" is not necessary to learn. I do not agree with this! If you want to learn how to program and so it would be easy and fast (well, in the manner of OOP), learn C # or Java for example. If you want to start learning lower-level programming right away, then "C"! - Jakeroid
  • thanks to all! I figured it out. even managed to roll up the encryption utility :) - sudo97

2 answers 2

In your program, it is assumed that the first argument is the name of the file to be opened or if it failed to print an error message.

argc, argv [] - This is how the OS sends call parameters to the program.

For example, you executed the gcc -o myprog tc command. In tc, a program with main (int ac, char * av []) in C.

 #include <stdio.h> #include <stdlib.h> main (int ac, char *av[]) { int i; for (i = 0; i < ac; i++) printf ("arg[%d] = '%s'\n",i,av[i]); exit(0); } 

The compiler will create a boot module (executable file ... well, or as it is called in textbooks ???) with the name myprog (in Windows myprog.exe) in the current table of contents.

Perform it by typing the command ./myprog a1 a2 a3 (on Windows myprog a1 a2 a3) the program will print:

 arg[0] = 'myprog' arg[1] = 'a1' arg[2] = 'a2' arg[3] = 'a3' 

Those. it prints its name (av [0]) and the arguments specified in the call string. The OS sends the number of arguments (ac) and the arguments themselves (av) to the program as a vector of string addresses (an array of pointers to sequences of characters terminated by a binary zero). The first argument is the name of the command (the path to it. Call out from different tables of contents and look not print).

By the way, the parameter address vector ends with a zero address, i.e. av [ac] == NULL.

    argv is an array of parameter strings passed when the program is started (parameters are written after the address in the command line, specified in the shortcut settings, or in the debugger settings, separated by a space). argc is the number of parameters passed. The zero parameter (argv [0]) is the address of the exe-file being launched.

    The program does nothing really sensible. First, it checks if the parameters are transmitted when the program starts. If argc <2, then argv contains only one element (the address of the exe-file being launched) and no parameters are passed. The string "arhc_gv.exe <file> is returned, returns 1. If we did not finish the execution at this stage, a stream is opened for writing to the file (the file address is taken from the first passed parameter argv [1]). If an error occurred while opening the stream, it displays the line "error opening file" returns 3. If we have reached the end of the program, close the stream.