C 为什么';t我的shell程序打开它在函数“中作为参数接收的文件”;猫;
我包括了下面的代码,很抱歉用这么多代码打扰你。参数解析没问题,我用手表检查过了。我放了一些C 为什么';t我的shell程序打开它在函数“中作为参数接收的文件”;猫;,c,shell,cat,C,Shell,Cat,我包括了下面的代码,很抱歉用这么多代码打扰你。参数解析没问题,我用手表检查过了。我放了一些printfs来检查问题可能在哪里,它似乎没有打开文件catreceives as参数。它从shell中被称为“cat-b文件”” #包括 #包括 #包括 #包括 #定义真0 #定义FALSE 1 void yes(int argc,char*argv[]); int cat(int-argc,char*argv[]); //#包括“h类” //#包括“是的,h” //#包括“tee.h” char-buf
printf
s来检查问题可能在哪里,它似乎没有打开文件cat
receives as参数。它从shell中被称为“cat-b文件”
”
#包括
#包括
#包括
#包括
#定义真0
#定义FALSE 1
void yes(int argc,char*argv[]);
int cat(int-argc,char*argv[]);
//#包括“h类”
//#包括“是的,h”
//#包括“tee.h”
char-buf[50],c[10],*p2,*p,*pch;
整数计数;
字符*矩阵[20];
void yes(int argc,char*argv[])
{
int i;
//如果(argc>=2&&*argv[1]='-')
// {
//printf(“错误!”);
//}
//如果(argc==1)
// {
而(1)
if(put(“y”)==EOF)
{
佩罗尔(“是”);
退出(假);
}
// }
而(1)
对于(i=1;i\n”);
而((c=getopt(argc,argv,“bnsE”)!=-1)
开关(c)
{
案例“b”:
数字=1;
打破
案例“n”:
数量=2;
打破
案例“m”:
标记=1;
打破
案例s:
挤压=1;
打破
案例“E”:
标记=1;
打破
}
如果(选项nd+1!=argc)
{
fprintf(stderr,“\t强参数!\n”);
返回-1;
}
文件*fd=fopen(argv[optind],“r”);
printf(“am deschis fisierul%s”,argv[optind]);
如果(fd==NULL)
{
printf(“FISIER NULL asdasdasd”);
返回1;
}
字符行[1025];
int line_count=1;
而(!feof(fd))
{
fgets(第1025行,fd);
printf(“插入时:>%s”,行);
int len=strlen(直线);
如果(第[len-1]行='\n')
{
如果(len-2>=0)
{
如果(第[len-2]行='\r')
{
第[len-2]行='\0';
len-=2;
}
其他的
{
行[len-1]='\0';
len-=1;
}
}
其他的
{
行[len-1]='\0';
len-=1;
}
}
如果(挤压==1&&len==0)
继续;
如果(数字==1)
{
fprintf(标准输出,“%4d”,行计数);
行数++;
}
else if(数字=2)
{
如果(len>0)
{
fprintf(标准输出,“%4d”,行计数);
行数++;
}
其他的
fprintf(标准符号“”);
}
fprintf(标准输出,“%s”,行);
如果(标记==1)
fprintf(标准输出,“$”);
fprintf(标准输出,“\n”);
}
fclose(fd);
返回0;
}
问题在于,在使用时,它会将换行符包含在字符串中。因此,当您传递文件名时,它会附加一个\n
字符,该字符是在输入命令时输入的。因此,传递的文件名不正确。了解新线可能在这些线上(只是一个指针):
希望这有帮助
PS:调试过程当
fopen
失败时,设置errno
。考试
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define TRUE 0
#define FALSE 1
void yes(int argc, char *argv[]);
int cat(int argc, char *argv[]);
//#include "cat.h"
//#include "yes.h"
//#include"tee.h"
char buf[50],c[10], *p2,*p, *pch;
int count;
char *matrix[20];
void yes(int argc, char *argv[])
{
int i;
// if (argc >= 2 && *argv[1] == '-')
// {
//printf("ERROR!");
//}
//if (argc == 1)
// {
while (1)
if (puts("y") == EOF)
{
perror("yes");
exit(FALSE);
}
// }
while (1)
for (i = 1; i < argc; i++)
if (fputs(argv[i], stdout) == EOF || putchar(i == argc - 1 ? '\n' : ' ') == EOF)
{
perror("yes");
exit(FALSE);
}
//exit(TRUE);
}
int main(int argc, char *argv[])
{
//p=(char *)malloc(sizeof(char)*50);
do
{
fprintf (stderr, "$ ");
fgets (buf,50,stdin);
p=buf;
fprintf (stderr, "Comanda primita de la tastatura: ");
fputs (buf, stderr);
int i=0,j=0;
//strcpy(p,buf);
strcpy(c,"\0");
while (buf[i] == ' ')
{
i++;
p++;
}
if (buf[i] == '#')
fprintf (stderr, "Nici o comanda, ci e un comentariu!\n");
else
{
j=0;
while (buf[i] != ' ' && buf[i] != '\n')
{
i++;
j++;
}
strncpy (c,p,j);
fprintf (stderr, "%s\n",c);
if (strcmp (c,"yes") == 0)
{
p2 = p+j+1;
pch = strtok (p2," ");
count = 0;
while (pch != NULL)
{
//printf ("%s\n",pch);
matrix[count] = strdup(pch);
pch = strtok (NULL, " ");
count++;
}
yes(count, matrix);
fprintf (stderr, "Aici se va executa comanda yes\n");
}
else if (strcmp (c,"cat") == 0)
{
p2 = p+j+1;
pch = strtok (p2," ");
count = 0;
while (pch != NULL)
{
//printf ("%s\n",pch);
matrix[count] = strdup(pch);
pch = strtok (NULL, " ");
count++;
}
cat(count,matrix);
fprintf (stderr, "Aici se va executa comanda cat \n");
}
else if (strcmp (c,"tee") == 0)
{
//tee();
fprintf(stderr, "Aici se va executa comanda tee\n");
}
fprintf (stderr, "Aici se va executa comanda basename\n");
strcpy(buf,"\0");
}
}
while (strcmp(c, "exit") != 0);
fprintf (stderr, "Terminat corect!\n");
return 0;
}
int cat(int argc, char *argv[])
{
int c ;
opterr = 0 ;
optind = 0 ;
char number = 0;
char squeeze = 0;
char marker = 0;
fprintf(stderr,"SALUT< SUNT IN FUNCTIZE>\n");
while ((c = getopt (argc, argv, "bnsE")) != -1)
switch (c)
{
case 'b' :
number = 1;
break;
case 'n' :
number = 2;
break;
case 'm' :
marker = 1;
break;
case 's' :
squeeze = 1;
break;
case 'E' :
marker = 1;
break;
}
if (optind + 1 != argc)
{
fprintf (stderr, "\tWrong arguments!\n") ;
return -1 ;
}
FILE * fd = fopen (argv[optind], "r");
printf("am deschis fisierul %s ",argv[optind]);
if (fd == NULL)
{
printf("FISIER NULL asdasdasdasdasd");
return 1;
}
char line[1025];
int line_count = 1;
while (!feof(fd))
{
fgets(line, 1025, fd);
printf("sunt in while :> %s",line);
int len = strlen(line);
if (line[len - 1] == '\n')
{
if(len - 2 >= 0)
{
if(line[len - 2] == '\r')
{
line[len - 2] = '\0';
len -= 2;
}
else
{
line[len - 1] = '\0';
len -= 1;
}
}
else
{
line[len - 1] = '\0';
len -= 1;
}
}
if (squeeze == 1 && len == 0)
continue;
if (number == 1)
{
fprintf (stdout, "%4d ", line_count);
line_count++;
}
else if (number == 2)
{
if (len > 0)
{
fprintf (stdout, "%4d ", line_count);
line_count++;
}
else
fprintf (stdout, " ");
}
fprintf(stdout, "%s", line);
if (marker == 1)
fprintf(stdout, "$");
fprintf(stdout, "\n");
}
fclose (fd);
return 0 ;
}
char *filename = strtok(argv[optind], "\n");
if( filename == NULL)
{
/*What the .... */
Handle error!
}
FILE * fd = fopen (filename, "r");
printf("am deschis fisierul %s ",argv[optind]);
if (fd == NULL)
{
printf("FISIER NULL asdasdasdasdasd");
return 1;
}
|hello.txt
|