所以我必须用C语言编写一个程序,它实际上是从键盘上取得一个命令,将它分成存储在一个数组中的标记,并将这些标记作为input到“execv”(一个在Ubuntu的命令),我select了命令“uname “与参数”-a“,但我卡在数组存储,因为它只存储被分割的令牌的第一个字母。
#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("%sn",c); // printing them c=strtok(NULL,",.!?"); } printf("Sunt %d elemente stocate in array! nn",i); //no of tokens stored printf("Primul cuvant este: nnn"+*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); /* asteapta dupa copil */ } //getch(); return 0; }
全球自创的对象
使用连接到LDAP服务器的生物识别系统authentication窗口用户
为什么没有/ dev / video1触发器select?
'st_blksize':不是Windows上'stat'的成员
我认为你的问题是printf("Primul cuvant este: nnn"+*cuvinte[1]); 声明。 这个解除了cuvinte第二个项目的第一个字符。 我想你想要做的是printf("Primul cuvant este: %s nnn",cuvinte[0]); 。
Live版本 。
您将strtok的输出(通过指针)分配给单个字符变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。