2

入力を解析してコマンドと引数に分離しようとしています。分割することはできますが、多数の文字列を含む配列に代入しようとすると問題が発生します。例:

cmd1<infile |cmd2 -a -b -c|cmd3 -x -y -z>outfile

引数を使用してコマンドに分割しても問題ありませんが、最終的に配列を印刷すると正しい出力が表示されず、 c での char* の動作を理解できません。

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

char* Getfile(char *input,int flag)
{
int i,k;
char *file;
//printf("\n++++++++++++++++++++++++++");
//puts(input);
if(strcmp("exit",input)==0)
exit(0);

 for(i=0;input[i]!='\0';i++)
 {
  if(flag==0 && input[i]=='<')
  break;
  else if(flag==1 && input[i]=='>'  )
  {
   if(input[i-1]!='2')
   break;
   else if(input[i-1]=='2' && input[i-2]!=' ' )
   break;
  }
  else if(flag==2 && input[i]=='>' && input[i-1]=='2' && input[i-2]==' ' )
  break;
 }//for

 if(input[i]==NULL)
 return NULL;

 i++;

 //cmd f1  2>f2
 for(k=0;input[i]!='>' && input[i]!='<'&& input[i]!=NULL ;i++,k++)//dont include ' ' cond otherwise cmd< f1 will not work
 {
   if(input[i]==' ' && input[i+1]=='2' && input[i+2]=='>')
   break;

   file[k]=input[i];
  // printf("\n %d %c",i,input[i]); //  getch();
  }
 file[k]=NULL;

//  printf("\n return file%d is \n",flag);
//  puts(file);
  //getch();
  return file;
}//function
//--------------------------------------------------------
char *Getcommand(char *input)
{
char *cmd;
int i,k;
for(k=0,i=0;input[i]!='>' && input[i]!='<'&& input[i]!=NULL;i++,k++)
 {
   cmd[k]=input[i];
   //printf(" %c",cmd[k]);
 }//for
 if(input[i]=='>' && input[i-1]=='2' &&input[i-2]==' ' )
 cmd[k-2]=NULL;
 cmd[k]=NULL;

 //printf("\ncommand is");
// puts(cmd);
// getch();;
 return cmd;
}
//-------------------------------------------------------


void main()
{
/*
char *args[10][20]={
{"ls","-a","-i",(char*)NULL},
{"sort",(char*)NULL},
{"wc","-c",(char*)NULL}
}; */

char *args[10][20],*input,*commands[10],*temp[10];
int i=0,k=0,noc=-1;
char *inputcopy,*inputcopy2,*inputfile,*outputfile,*errorfile[10];
clrscr();
//------------
printf("enter string\n");
gets(input);
strcpy(inputcopy,input);
commands[0]=strtok(inputcopy,"|");

for(i=0;commands[i]!=NULL;)
{ commands[++i]=strtok(NULL,"|");
}
noc=i-1; //no of commands brgin from 0

strcpy(inputfile,Getfile(commands[0],0));
strcpy(outputfile,Getfile(commands[noc],1));

i=0;
while(i<=noc)
{
 strcpy(inputcopy,Getcommand(commands[i]));
  k=0;
  //----
 args[i][k]=strtok(inputcopy," ");
 while(args[i][k]!=NULL)
 args[i][++k]=strtok(NULL," ");
 //------- immediate printing show args[i][k] has correct value :/ -----------
 for(k=0;args[i][k]!=NULL;k++)
 printf("\n --args[%d][%d]=  %s ",i,k,args[i][k]);
// getch();
 //-----
i++;
}
//---------
printf("\n outputfile=%s",outputfile);
printf("\n inputfile=%s",inputfile);
//===========but here actual printing is not showing correct output=====
i=0;
while(i<=noc)
{
 printf("\n**********comands no %d *******",i);
 for(k=0;args[i][k]!=NULL;k++)
 printf("\n args[%d][%d] %s ",i,k,args[i][k]);

 getch();
 i++;
}

//------
getch();

}//main
4

1 に答える 1

1

char *File を宣言しましたが、それにスペースを割り当てませんでした。メモリ内のスペースを指している配列にコンテンツをコピーしてから展開しようとしていますが、未定義の動作だと思います。そのためのスペースを作成するには、malloc/calloc を使用します。

    file = (char * ) malloc(sizeof(char) * maxLength);

配列の maxLength を定義し、for ループで内容をコピーします。

   for(k=0;input[i]!='>' && input[i]!='<'&& input[i]!=NULL && k < maxLength ;i++,k++)//dont include ' ' cond otherwise cmd< f1 will not work
   {
       if(input[i]==' ' && input[i+1]=='2' && input[i+2]=='>')
       break;

       file[k]=input[i];
       // printf("\n %d %c",i,input[i]); //  getch();
   }
于 2013-10-23T21:20:07.500 に答える