我无法访问 c 中的 if 语句
I can not access to if statement in c
我试图访问 append
、in
和 out
的 if
语句,但目前只有“追加”有效。
我不知道为什么它对 in
和 out
不起作用。你能告诉我问题是什么吗?
如果我键入“ls > hi.txt”,args
的内容是:
args[0] = ls
args[1] = >
args[2] = hi.txt
args[3] ='[=10=]'
for(i = 0; args[i] != (char*)'[=11=]';i++)
{
if(strcmp(args[i],"<")==0)
{
args[i] = NULL;
printf("IAMHERE\n");
strcpy(input,args[i+1]);
in = 2;
}
if(strcmp(args[i],">")==0)
{
args[i] = NULL;
printf("IAMHERE\n");
strcpy(output,args[i+1]);
out = 2;
}
if(strcmp(args[i],">>")==0)
{
args[i] = NULL;
strcpy(output,args[i+1]);
append = 2;
}
}
if(append)
{
printf("yohere\n");
if((fap = open(output,O_RDWR|O_APPEND))<0)
{
perror("Could not open outputfile");
exit(0);
}
dup2(fap,STDOUT_FILENO);
close(fap);
}
if(in)
{
printf("yohere\n");
if((fin = open(input,O_RDONLY,0))<0){
perror("Couldn't open input file");
exit(0);
}
dup2(fin,0);
close(fin);
}
if(out)
{
printf("yohere\n");
if((fout = creat(output,0644))<0){
perror("Could not open the outputfile");
exit(0);
}
dup2(fout,STDOUT_FILENO);
close(fout);
}
你的 for
循环中有(潜在的)未定义行为:第二个和第三个 if
语句应该是 else if
,而不是(毕竟,只有三种可能性中的一种其实都能满足)。
就目前而言,当执行 first if
块 (if(strcmp(args[i],"<")==0)
) 时,该块中的代码将 argv[i]
设置为NULL
指针,然后用作 second if
测试中 strcmp
的第一个参数。对 strcmp
的调用会导致未定义的行为,因为 NULL
不是 指向 nul
- 的指针终止字符串。
进入secondif
块时会出现类似的情况:third中的测试会导致undefined行为。
来自 cppreference:
The behavior is undefined if lhs or rhs are not pointers to
null-terminated byte strings.
因此,当您的运算符是 >>
(“追加”)时,不会调用未定义的行为,因为前两个 if
块都没有被执行;但是,对于 <
或 >
,输入其中一个 是 ,argv[i]
设置为 NULL
,并且出现 UB; UB 可能 包括 strcmp
函数 'incorrectly' 返回零,因此其他 if
块中的一个或两个将被执行,导致您的程序给出错误的结果。
我试图访问 append
、in
和 out
的 if
语句,但目前只有“追加”有效。
我不知道为什么它对 in
和 out
不起作用。你能告诉我问题是什么吗?
如果我键入“ls > hi.txt”,args
的内容是:
args[0] = ls
args[1] = >
args[2] = hi.txt
args[3] ='[=10=]'
for(i = 0; args[i] != (char*)'[=11=]';i++)
{
if(strcmp(args[i],"<")==0)
{
args[i] = NULL;
printf("IAMHERE\n");
strcpy(input,args[i+1]);
in = 2;
}
if(strcmp(args[i],">")==0)
{
args[i] = NULL;
printf("IAMHERE\n");
strcpy(output,args[i+1]);
out = 2;
}
if(strcmp(args[i],">>")==0)
{
args[i] = NULL;
strcpy(output,args[i+1]);
append = 2;
}
}
if(append)
{
printf("yohere\n");
if((fap = open(output,O_RDWR|O_APPEND))<0)
{
perror("Could not open outputfile");
exit(0);
}
dup2(fap,STDOUT_FILENO);
close(fap);
}
if(in)
{
printf("yohere\n");
if((fin = open(input,O_RDONLY,0))<0){
perror("Couldn't open input file");
exit(0);
}
dup2(fin,0);
close(fin);
}
if(out)
{
printf("yohere\n");
if((fout = creat(output,0644))<0){
perror("Could not open the outputfile");
exit(0);
}
dup2(fout,STDOUT_FILENO);
close(fout);
}
你的 for
循环中有(潜在的)未定义行为:第二个和第三个 if
语句应该是 else if
,而不是(毕竟,只有三种可能性中的一种其实都能满足)。
就目前而言,当执行 first if
块 (if(strcmp(args[i],"<")==0)
) 时,该块中的代码将 argv[i]
设置为NULL
指针,然后用作 second if
测试中 strcmp
的第一个参数。对 strcmp
的调用会导致未定义的行为,因为 NULL
不是 指向 nul
- 的指针终止字符串。
进入secondif
块时会出现类似的情况:third中的测试会导致undefined行为。
来自 cppreference:
The behavior is undefined if lhs or rhs are not pointers to null-terminated byte strings.
因此,当您的运算符是 >>
(“追加”)时,不会调用未定义的行为,因为前两个 if
块都没有被执行;但是,对于 <
或 >
,输入其中一个 是 ,argv[i]
设置为 NULL
,并且出现 UB; UB 可能 包括 strcmp
函数 'incorrectly' 返回零,因此其他 if
块中的一个或两个将被执行,导致您的程序给出错误的结果。