main 函式中引數 argc和 argv 的設定
該篇文章主要是關於C++\C語言最基礎的main函式的引數知識,是學習C++或C語言都必備的知識點.不知道你是否知道該知識?希望對大家有所幫助.
一.main()函式引數
通常我們在寫主函式時都是void main()或int main() {..return 0;},但ANSI-C(美國國家標準協會,C的第一個標準ANSI釋出)在C89/C99中main()函式主要形式為:
(1).int main(void)
(2).int main(int argc,char *argv[]) = int main(int argc,char **argv).
其引數argc和argv用於執行時,把命令列引數傳入主程式.其中ARG是指arguments,即引數.具體含義如下:
(參照
(1).int argc:英文名為arguments count(引數計數)
count of cmd line args,執行程式傳送給main函式的命令列引數總個數,包括可執行程式名,其中當argc=1時表示只有一個程式名稱,此時儲存在argv[0]中.
(2).char **argv:英文名為arguments value/vector(引數值)
pointer to table of cmd line args,字串陣列,用來存放指向字串引數的指標陣列,每個元素指向一個引數,空格分隔引數,其長度為argc.陣列下標從0開始,argv[argc]=NULL.
argv[0] 指向程式執行時的全路徑名
argv[1] 指向程式在DOS命令中執行程式名後的第一個字串
argv[2] 指向執行程式名後的第二個字串
argv[argc] 為NULL.
二.原始碼中的argc與argv
由於C程式必須有main()函式為入口,而且它不能被其他函式呼叫(可以呼叫自身),因此不能再程式內部取得實際值.那麼在何處把實參賦值給main函式的形參呢?這就需要呼叫”執行”或”DOS提示符”,在呼叫可執行程式exe時,編譯器會幫助我們將輸入引數的資訊放入main函式的引數列表中傳參.
1.計算命令列引數個數程式如下:
- <strong>//C 輸出引數個數
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc,char *argv[])
- {
- printf(”引數個數=%d\n”,argc);
- system(”PAUSE”);
- return 0;
- }
- //C++ 輸出引數個數
- #include <iostream>
- usingnamespace std;
- int main(int argc,char *argv[])
- {
- cout<<”引數個數=”<<argc<<endl;
- system(”PAUSE”);
- return 0;
- }</strong>
<strong>//C 輸出引數個數
include <stdio.h>
include <stdlib.h>
int main(int argc,char *argv[])
{
printf("引數個數=%d\n",argc);
system("PAUSE");
return 0;
}
//C++ 輸出引數個數
include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
cout<<"引數個數="<<argc<<endl;
system("PAUSE");
return 0;
}</strong>
呼叫”執行”(快捷鍵Ctrl+R)或”cmd”輸入”G:\test.exe”會輸出”引數個數=1”,此時儲存的就是執行程式名.輸入”G:\test.exe 2 hello good”輸出”引數個數=4”:
2.檢視argv[]二維陣列儲存具體字串的程式碼如下:
[cpp] view plain copy print?- <strong>//C 檢視argv儲存引數值及對應序號
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc,char *argv[])
- {
- int i;
- printf(”引數個數=%d\n”,argc);
- for(i=0; i<argc; i++)
- {
- printf(”引數序號=%d ”,i);
- printf(”引數值=%s\n”,argv[i]);
- }
- system(”PAUSE”);
- return 0;
- }
- //C++ 檢視argv儲存引數值及對應序號
- #include <iostream>
- usingnamespace std;
- int main(int argc,char *argv[])
- {
- cout<<”引數個數=”<<argc<<endl;
- for(int i=0; i<argc; i++)
- {
- cout<<”引數序號=”<<i<<“ ”;
- cout<<”引數值=”<<argv[i]<<endl;
- }
- system(”PAUSE”);
- return 0;
- }</strong>
<strong>//C 檢視argv儲存引數值及對應序號
include <stdio.h>
include <stdlib.h>
int main(int argc,char *argv[])
{
int i;
printf("引數個數=%d\n",argc);
for(i=0; i<argc; i++)
{
printf("引數序號=%d ",i);
printf("引數值=%s\n",argv[i]);
}
system("PAUSE");
return 0;
}
//C++ 檢視argv儲存引數值及對應序號
include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
cout<<"引數個數="<<argc<<endl;
for(int i=0; i<argc; i++)
{
cout<<"引數序號="<<i<<" ";
cout<<"引數值="<<argv[i]<<endl;
}
system("PAUSE");
return 0;
}</strong>
“執行”中輸入”G:\test.exe 2 hello good”,則輸出入下圖所示:
其中argv[0]指向字串可執行程式的名稱G盤下的test.exe,通常會位於”專案名稱\Debut\xxx.exe”中.後面argv[1..3]單元依次指向程式呼叫時的引數.
三.呼叫argc和argv
在”執行”中輸入”notepad.exe”回車能執行記事本程式(位於C:\Windows\System32),如果輸入”notepad.exe test.txt”可以開啟test.txt文字檔案,其中test.txt位於當前路徑下.如下圖所示:
為什麼我要講述這個例子呢?主要是說明引數與exe之間的關係,main()函式其實與之也類似.同時在使用檔案知識時,我們通常會涉及到main函式的argc和argv引數.如在《C++ Primer》這本書中第10.3.9實現單詞轉換的例子就涉及到該運用,這裡只講述涉及到該引數的部分程式碼供大家參考,大家可以自己去學習瞭解:
- #include <iostream>
- usingnamespace std;
- int main(int argc,char *argv[])
- {
- //引數個數=3:工程名\讀取txt檔案\寫入txt檔案
- if(argc!=3)
- throw runtime_error(“wrong number of arguments”);
- //開啟轉換檔案,argv[1]為讀取的要轉換txt檔名 open_file自定義開啟函式
- if(!open_file(map_file,argv[1]))
- throw runtime_error(“no transformation file”);
- //開啟要轉換的寫入txt檔案
- if(!open_file(input,argv[2]))
- throw runtime_error(“no input file”);
- return 0;
- }
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
//引數個數=3:工程名\讀取txt檔案\寫入txt檔案
if(argc!=3)
throw runtime_error("wrong number of arguments");
//開啟轉換檔案,argv[1]為讀取的要轉換txt檔名 open_file自定義開啟函式
if(!open_file(map_file,argv[1]))
throw runtime_error("no transformation file");
//開啟要轉換的寫入txt檔案
if(!open_file(input,argv[2]))
throw runtime_error("no input file");
return 0;
}
其中具體操作是通過map(鍵-值對的集合)物件來實現,推薦大家看看這本最經典的C++書籍.
最後希望該文章對大家有所幫助,請不要小看這簡單的基礎知識,如果有錯誤或不足之處,還請海涵.
(By:Eastmount 2014-3-5 夜2點 原創:http://blog.csdn.net/eastmount)
</div>