ç课程校解析器的Ubuntu? [英] C Progam parser for ubuntu?
问题描述
所以,我必须建立在C程序,实际上从键盘使用的命令,将其分割成存储在一个阵列标记并使用这些标记作为输入execv(在Ubuntu的命令),我选择了命令的uname与参数-a,但我被困在阵列中存储,因为它仅存储这是分裂令牌的第一个字母。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT; / *的strtok的strcpy * /
#包括LT&;&malloc.h所GT; / * malloc的* /
#包括LT&; SYS / types.h中> / * *将为pid_t /
#包括LT&; SYS / wait.h> / * waitpid函数* /
#包括LT&;&unistd.h中GT; / * _exit,叉* /诠释的main()
{
INT I = 0;
字符* cuvinte [256]; //词
焦炭comanda [256]; //命令 的printf(Introduceti comanda:); //输入命令
与fgets(comanda,sizeof的(comanda),标准输入); //读取命令
字符* C = strtok的(comanda,); //把它分解成标记 而(C!= 0)
{
cuvinte [I] = malloc的(strlen的(C)+ 1); // alocate存储器阵列
的strcpy(cuvinte [我++],C); //令牌复制到数组
的printf(%S \\ n,C); //打印它们
C = strtok的(NULL,,!?);
}
的printf(必须遵守%d个元素系列stocate在阵列\\ n \\ n!,我); //没有令牌存储
的printf(Primul cuvant埃斯特:\\ n \\ n \\ n+ * cuvinte [1]); //应该打印的第一个字
/ *我来到这里stucked因为令牌* // *脸上未PROCES copil * /
将为pid_t PID =叉();
如果(PID == 0){/ * procesul copil * /
静态的char * argv的[] = {/斌/的uname, - 一,NULL};
execv(的argv [0],argv的);
出口(127); / *在CAZ CA execv DA失败* /
}
其他{/ * PID = 0!; PROCES parinte * /
waitpid函数(PID,0,0); / * asteapta DUPA copil * /
}
//残培();
返回0;
}
我觉得你的问题是的printf(Primul cuvant埃斯特:\\ n \\ n \\ n+ * cuvinte [1]) ;
语句。这解引用的第二个项目的第一个字符 cuvinte
。我想你想要做的,而不是为的printf(Primul cuvant埃斯特:%S \\ n \\ n \\ n,cuvinte [0]);
真人版。
So I have to build a program in C that practically takes a command from keyboard , split it into tokens that are stored in an array and use those tokens as input to "execv" (a command in ubuntu) , I chose the command "uname" with the parameter "-a", but I got stuck at the array storing, as it stores only the first letter of the token which was split.
#include <stdio.h>
#include<stdlib.h>
#include <string.h> /*strtok strcpy*/
#include<malloc.h> /*malloc*/
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* waitpid */
#include <unistd.h> /* _exit, fork */
int main()
{
int i=0;
char *cuvinte[256]; // words
char comanda[256]; //command
printf("Introduceti comanda: "); //enter the command
fgets(comanda,sizeof(comanda),stdin); // read the command
char *c = strtok(comanda," "); // split it into tokens
while(c!=0)
{
cuvinte[i] = malloc( strlen( c ) + 1 ); // alocate memory for the array
strcpy(cuvinte[i++],c); //copying tokens into array
printf("%s\n",c); // printing them
c=strtok(NULL, " ,.!?");
}
printf("Sunt %d elemente stocate in array! \n\n",i); //no of tokens stored
printf("Primul cuvant este: \n\n\n"+*cuvinte[1]); //should print the first word
/*i got stucked here because of the tokens */
/*face un proces copil*/
pid_t pid=fork();
if (pid==0) { /* procesul copil*/
static char *argv[]={"/bin/uname","-a",NULL};
execv(argv[0],argv);
exit(127); /*in caz ca execv da fail*/
}
else { /* pid!=0; proces parinte */
waitpid(pid,0,0); /* asteapta dupa copil */
}
//getch();
return 0;
}
I think your problem is the printf("Primul cuvant este: \n\n\n"+*cuvinte[1]);
statement. This dereferences the first character of the second item in cuvinte
. What I think you want to do instead is printf("Primul cuvant este: %s \n\n\n",cuvinte[0]);
.
这篇关于ç课程校解析器的Ubuntu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!