Visal Studioでコマンドライン引数を使う Use command line argumants on Visual Studio
はじめに Introduction
プログラム実行時にコマンドライン引数を付ける事があります. コマンドライン引数を使うとmain関数に引数を渡す事ができます. また, リダイレクトで標準入出力をキーボードからファイルへと切り換える事ができます.
Visual Studioでコマンドライン引数を設定する方法を紹介します.
You can execute a program with some command line arguments. With command line arguments, you can give parameters to the main function and change standard input/output into file from keyboard, in other words, redirection.
This article explains how to set command line arguments on Visual Studio.
問題 What's the problem ?
Visual Studioでプログラムをコンパイルし, 実行する時, 多くの場合実行ボタンや"ctrl"+"F5", "F5"で行うと思います. コマンドライン引数を設定したい場合どこに入力すれば良いのか, 最初私は分かりませんでした.
To compile and execute, usually we use execution button, or press "ctrl"+"F5", or "F5". When you must set command line arguments, where do you set them?
解決策 The Solution
Visual Studioでコマンドライン引数を設定する方法は次の通りです.
The following is how to set command line arguments on Visual Studio.
- Open "Project" --> "Properties"
- Select "Configuration Properties" --> "Debug"
- Enter command line arguments or redirection you want to add into "Command line arguments".
Visual Studio 2015で確認 Test the above on Visual Studio 2015
標準入力をSampleInput.txtへ, 標準出力をOutputForSampleInputへと切り換えます. SampleInput.txtの内容とmain関数に渡されたコマンドライン引数をOutputForSampleInput.txtに出力します.
Command line arguments to add changes standard input to SampleInput.txt from keyboard and changes standard output to OutputForSampleInput.txt. Program outputs contens of SampleInput.txt and given command line arguments.
追加するコマンドライン引数 Command line arguments to add
What I want to give to main <"SampleInput.txt" >"OutputForSampleInput.txt"
プログラムのソースコード Source code of the program
#include<iostream> using namespace std; int main(int argc, char *argv[]){ cout << "command line arguments : "; for (int i = 0; i < argc; ++i) { cout << argv[i] << " "; } cout << endl; int a, b; cin >> a >> b; cout << "sample input : " << a << " " << b; }
SampleInput.txt
10 89
OutputForSampleInput.txt
command line arguments : C:\YourProjectFolder\x64\Debug\ProgramName.exe What I want to give to main sample input : 10 89
OutputForSampleInput.txtを見ると成功していることが確認できます. OutputForSampleInput.txt shows success.
感想 Thoughts
Visual Studioで競技プログラミングの問題を解く時などに標準入出力をファイルに切り換えるとデバッグが楽になると思いました.
I thought that changing standard I/O to files makes debugging the source code of programming contest easy.