C-命令行参数
时间:2020-02-23 14:31:53 来源:igfitidea点击:
在本教程中,我们将学习在C编程语言中使用命令行参数。
什么是命令行参数?
命令行参数是调用它们时提供给程序的参数。
例如,假设我们有一个程序" copyfile",它带有两个参数,即文件名,其中,第一个文件名是源文件,第二个文件名是目标文件。
然后该程序将第一个文件的内容复制到第二个文件中。
视窗:
E:\TC\BIN>copyfile source_filename destination_filename
Mac/Linux:
$./copyfile source_filename destination_filename
main函数
每个C程序都具有main()函数,该函数标记代码执行的起点。
因此,到目前为止,我们尚未将任何参数传递给main()函数,因此我们设置了void。
int main(void) {
//some code goes here...
return 0;
}
argc和argv参数
main()函数可以接受两个参数,分别是argc和argv。
int main(int argc, char *argv[]) {
//some code goes here...
return 0;
}
argc是参数计数器,用于对在命令行上传递的参数数量进行计数。
argv是参数向量,表示指向命令行参数的字符指针数组。
并且该指针数组的大小等于" argc"的值。
在以下示例中,我们将执行copyfile程序并传递两个文件名(源和目标)。
Windows:
E:\TC\BIN>copyfile original.txt duplicate.txt
Mac/Linux:
$./copyfile original.txt duplicate.txt
因此,以上示例的" argc"为3。
指针" argv"的数组如下。
| 元素 | 值 |
|---|---|
| argv [0] | copyfile |
| argv [1] | original.txt |
| argv [2] | duplicate.txt |
注意!命令行参数的第一个参数始终代表程序名称。
用C编写一个程序,将内容从一个文件复制到下一个文件
/**
* file: copyfile.c
* date: 2011-04-20
* description:
* COMMAND LINE ARGUMENT
* user defined command: copyfile
* This command allows the user to copy the content of file1 to file2.
* if file2 already exists then overwrite only after taking user permission.
*
* syntax:
* $./copyfile <file1.extension_name> <file2.extension>
*
* where,
* file1 is the source file
* file2 is the target file where content of file1 is copied.
*
* example:
* $./copyfile original.txt duplicate.txt
*/
#include <stdio.h>
int main(int argc, char *argv[])
{
//file pointers
FILE
*fptr1,
*fptr2;
//variables
int i;
char ch;
printf("Total number of argument passed: %d\n", argc);
//open source file in read mode
//if error
//then, exit
if( (fptr1 = fopen(argv[1], "r") ) == NULL ) {
printf("Error...\nCannot open file: %s\n", argv[1]);
printf("Either the filename is incorrect or it does not exists.\n");
return -1;
}
//check if the destination file exists
if( (fptr2 = fopen(argv[2], "r") ) != NULL) {
printf("Warning: File %s already exists.\n", argv[2]);
printf("Press Y to overwrite. Or any other key to exit: ");
//take user input
ch = getchar();
//if user don't want to overwrite
//then, exit
if(ch != 'Y' && ch != 'y') {
printf("Terminating the copy process.\n");
//close connection
fclose(fptr1);
fclose(fptr2);
return -1;
}
else {
//close read mode connection
fclose(fptr2);
//now open destination file in write mode
fptr2 = fopen(argv[2], "w");
}
}
//if destination file doesn't exists
//then, open destination file in write mode
else {
fptr2 = fopen(argv[2], "w");
}
//copy the content of source to destination
while( !feof(fptr1) ) {
ch = getc(fptr1);
if(ch != EOF) {
putc(ch, fptr2);
}
}
//close connection
fclose(fptr1);
fclose(fptr2);
printf("Content of %s copied to %s\n", argv[1], argv[2]);
return 0;
}
第一次运行。
-MBP:c-project $gcc copyfile.c -o copyfile -MBP:c-project $./copyfile original.txt duplicate.txt Total number of argument passed: 3 Content of original.txt copied to duplicate.txt
第二次运行。
-MBP:c-project $./copyfile original.txt duplicate.txt Total number of argument passed: 3 Warning: File duplicate.txt already exists. Press Y to overwrite. Or any other key to exit: y Content of original.txt copied to duplicate.txt
再次运行,但是这次终止了进程。
-MBP:c-project $./copyfile original.txt duplicate.txt Total number of argument passed: 3 Warning: File duplicate.txt already exists. Press Y to overwrite. Or any other key to exit: n Terminating the copy process.
文件内容。

