※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Correspondence to drug   

  1. ..clause 1..: Acquisition of dragged file name
  2. ..clause 2..: Analysis of option

[1] Acquisition of dragged file name

It came to be able to do a minimum file processing by the explanation to the preceding clause.
However, it is troublesome to input the file name every time.
It is very convenient if it drags from the Explorer etc. and it comes to open it.

Actually, on the command prompt screen of Windows
If the file is dragged, the file name is automatically input.
If it is this method, the file name can be easily input.
However, when the file is dragged in the execution file, it is not possible to do.

In C language, the function to pass the file name when the application is started is provided.
It is a command line argument.


[   Command line argument   ]
Character string passed when application starts
Specify the file name and the optional operation chiefly processed.
Though the argument of the main function has been declared up to now as void type
Actually, the argument of the decided type can be specified for here.
Specify the following arguments to receive the command line argument.
 int main(int argc,char *argv[]);
Argc is a number of command lines, and argv is pointer variable to the character array.
Though the usage seems to be difficult when it ..character array.. is heard the pointer variable
Actually only do as follows.

printf("%s",argv[0]);
The command line 0(..the first.. beginning) can be displayed if it does like this.
If the number of elements of argv is changed, it can access the command line corresponding to it.

The 0th command line becomes the file name of the application.
The file name dragged from the Explorer etc. is stored in the first.

The following program is an example of displaying the dragged file name.

 
#include <stdio.h> 

int main(int argc,char *argv[])  
{
	if (argc > 1) { printf("%s\
		n",argv[1]); 
	}
	
	fflush(stdin);  
	getchar();  
	
	return 0;
}
It is judged that the number of command lines is confirmed with argc, and there is a command line if it is more than one.
The content of the first command line is displayed.

	fflush(stdin);
	getchar();
Two lines above are the processing to stop the display of the screen. Become an execution waiting, and end with a suitable key.

[   Fflush(stdin) is evil ways   ].
The instruction above is a function that compelling outputs the output buffer With fflush
It is an input buffer Stdin is cleared.
This usage is evil ways that can be used only by a part of compiler.
Do not use this use only at the practice stage, and do not use it in real development.
The result of dragging a suitable file by the Explorer becomes as follows.

D:\BCPad\Source\test.exe
The name of the dragged file is displayed in the full path.

[ Position of   execution file   ]
To the same folder as the compiled source file in many cases
The execution file is made by the same name as the source file name.

It returns to contents.


[2] Analysis of option

Generally, besides the file name ..the command line...
The option that specifies the operation of the application might be specified.

For instance, in execution specifying the file name with Windows When starting specifying defrag
Though it ends without defrag's starting and doing anything
Begin the defrag of C drive when specifying defrag c.
Moreover, display only the analysis result of C drive when specifying defrag c:-a.
Such a usage is a familiar way for the personal computer senior.

In this example, two character strings named c and ..-.. a are passed to the command line.
It is analyzed in the application, and operation has been decided.

If the character string of the command line is examined, similar is easily understood.
..-.. ..a.. - here It is an analysis example as for the presence of the option of s.

 
#include <stdio.h> 

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

	while (argc > 0) { 
		
		argc--;  
		
		  If (argv[argc][1] =='a') printf ("Option \n of - a"); 
			  if (argv[argc][1] =='s') Printf ("Option \n of - 
			s");
		}
	}
	
	return 0;
}
..this program.. - The option named a -s is given and the result of execution is as follows.

- A is optional.
- S is optional.
It is possible to correspond to the option how many if keeping similar.
Moreover, if you treat the character string, that is, no - the head as a file name
The file name can be acquired as well as the preceding clause.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.