请大家帮我看一下这个C语言参数传递问题
来源:百度知道 编辑:UC知道 时间:2024/05/25 04:46:09
有一个Linux下的C程序useupper.c
/* This code, useupper.c, accepts a file name as an argument
and will respond with an error if called incorrectly. */
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *filename;
if(argc != 2) {
fprintf(stderr, "usage: useupper file\n");
exit(1);
}
filename = argv[1];
/* That done, we reopen the standard input, again checking for any errors as we do so,
and then use execl to call upper. */
if(!freopen(filename, "r", stdin)) {
fprintf(stderr, "could not redirect stdin to file %s\n", filename);
exit(2);
}
execl("./upper", "upper", 0);
/* Don't forget that execl replaces the current process;
provided there is no error, the remaining
/* This code, useupper.c, accepts a file name as an argument
and will respond with an error if called incorrectly. */
#include <unistd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char *filename;
if(argc != 2) {
fprintf(stderr, "usage: useupper file\n");
exit(1);
}
filename = argv[1];
/* That done, we reopen the standard input, again checking for any errors as we do so,
and then use execl to call upper. */
if(!freopen(filename, "r", stdin)) {
fprintf(stderr, "could not redirect stdin to file %s\n", filename);
exit(2);
}
execl("./upper", "upper", 0);
/* Don't forget that execl replaces the current process;
provided there is no error, the remaining
argc 是描述参数的个数
argv 是描述每个参数
./useupper file.txt canshu
则argc=2
argv={"file.txt","canshu"}
这两个参数是固化的,不是用户随便定义的
例如你编译好的程序叫做program.exe
你运行 program a b
这个时候,argc = 3
argv[0] = "program"
argv[1] = "a"
argv[2] = "b"
看不懂,