我试图访问 , 的语句,if但append现在只有“附加”有效。inout
我不知道为什么它不适用于inand out。你能告诉我有什么问题吗?
args如果我输入“ls > hi.txt”,内容是:
args[0] = ls
args[1] = >
args[2] = hi.txt
args[3] ='\0'
for(i = 0; args[i] != (char*)'\0';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);
}