找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2073|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

C Primer Plus 程序清單11.27中VS2010關(guān)于行命令參數(shù)的設(shè)置

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:105323 發(fā)表于 2016-2-23 15:38 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
對于新手,可能不知道如何操作vs2010使之達(dá)到編譯的預(yù)期效果,具體步驟如下
新建項目,c++編寫程序如下:
#include
int main(int argc, char *argv[])
{
int count;
printf("The command line has %d arguments: \n",argc-1);
for(count = 1;count < argc; count++)
printf("%d: %s\n",count,argv[count]);
printf("\n");
getchar();
getchar();
return 0;
}
為了實現(xiàn)程序中的命令行參數(shù)argv[]的使用,需要設(shè)置一下項目的屬性。
項目->屬性->調(diào)試->命令參數(shù),輸入 Resistance is futile
如下圖所示

?


編譯執(zhí)行程序即可。
結(jié)果如下:



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:105323 發(fā)表于 2016-2-23 15:43 | 只看該作者
上篇寫到程序清單11.27的相關(guān)VS2010的相關(guān)操作,13-2的程序相對要復(fù)雜一點,故此說明。程序如下
// reducto.c -- reduces your files bytwo-thirds!
#include
#include
#include
#define LEN 40
int main(int argc, char *argv[])
{
FILE *in, *out;
int ch;
char name[LEN];
int count = 0;
//check the command arguments
if(argc < 2)
{
fprintf(stderr,"Usage: %s filename\n",argv[0]);
getchar();
exit(1);
}
// finish the input action
if((in = fopen( argv[1], "r")) == NULL)
{
fprintf(stderr,"I couldn't open the file\"%s\"\n",argv[1]);
getchar();
exit(2);
}
// finish the output action
strcpy(name,argv[1]);//把文件名復(fù)制到數(shù)組中
strcat(name,".txt");//在文件名后添加.txt
if((out = fopen(name,"w"))== NULL)//檢查文件是否為空
{//打開文件以供寫入
fprintf(stderr,"Can't create output file.\n");
getchar();
exit(3);
}
// copy data
while((ch =  getc(in))!= EOF)
if(count++ % 3 == 0)
putc(ch,out);
// clear up
if(fclose(in)!= 0 ||fclose(out)!= 0)
fprintf(stderr,"Error in cliosing file.\n",argv[1]);
return 0;
}
與上篇不同的是在 command lines 輸入a ,還要設(shè)置路徑(workingdirectory)為桌面,以便大家方便地觀看結(jié)果,并原書中的“.red”替換成“.txt”,增加可讀性。也在exit();前添加上getchar();以便更加直觀地觀看error.


?還需在桌面建立一個a.txt,在文本中寫入 So even Eddy cameoven ready. 在通過屬性更改去掉.txt.
編譯完成,執(zhí)行。桌面上就會出現(xiàn)a.txt的新文本文件,打開文件中,就會出現(xiàn) Sendmoney? 。如果忘記新建桌面設(shè)置這樣的a文件,那木


?就會彈出對話框, I couldn't open the file"a"。

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表