Strtok()分段错误

时间:2013-10-26 00:35:39

标签: c segmentation-fault strtok

struct args
{
    char command[64];
    char args[2][64];
};

int argscount = 0;
struct args* arguments;
int buffersize = 64;
char *ptoken = NULL;
char input[buffersize];
char *pstr = NULL;



int a = read(0,input,buffersize);
pstr = input;
arguments = malloc(sizeof(struct args));        
if(a>0){  
    ptoken = strtok(&pstr," ");
    strcpy(arguments->command,ptoken);
    printf("TOKEN:%s\n", ptoken);
    while( ptoken != NULL ) {
        if(argscount > 1){break;}
        ptoken = strtok(NULL, ' ');
        strcpy(arguments->args[argscount],ptoken);
        argscount++;  
    }  
}

4 个答案:

答案 0 :(得分:3)

问题很可能出在

ptoken = strtok(&pstr," ");

strtok的第一个参数应该是

char *

你有

char **

答案 1 :(得分:2)

read不返回strtok期望的空终止字符串。您需要在输入中分配一个额外字节来添加'\0'。您可以查看read的返回值以查看已读取的字节数,然后将'\0'放在input[a]

int a = read(0,input,buffersize-1);
input[a] = '\0';

答案 2 :(得分:0)

代码中的一些修复(和其他)。评论描述了我改变的内容:(如果定义了read()函数,它可能已经构建)还添加了main,只是为了编译错误。

#include <ansi_c.h>

 struct args
{
    char command[64];
    char args[2][64];
};

int argscount = 0;
struct args* arguments;
size_t buffersize = 64; //changed to size_t
char *ptoken = NULL;
char input[64];  //variable initializer not allowed, changed
char *pstr = NULL;

int read(int a, char *s, size_t size);


main(void)
{

    int a = read(0,input,buffersize);

pstr = input;
arguments = malloc(sizeof(struct args));        
if(a>0)
    {  
        ptoken = strtok(pstr," "); //changed &pstr to pstr
        strcpy(arguments->command,ptoken);
        printf("TOKEN:%s\n", ptoken);
        while( ptoken != NULL ) {
            if(argscount > 1){break;}
            ptoken = strtok(NULL, " "); //changed ' ' to " "
            strcpy(arguments->args[argscount],ptoken);
            argscount++;  
        }  
    }
}

答案 3 :(得分:0)

这是一种正常,明智的做法:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct args {
    char command[64];
    char args[2][64];
};

int main(void) {
    char input[64] = {0};

    if ( read(0, input, sizeof(input) - 1) == -1 ) {
        perror("unsuccessful read() operation");
        return EXIT_FAILURE;
    }

    char * ptoken = strtok(input, " ");
    if ( ptoken == NULL ) {
        fprintf(stderr, "No valid input\n");
        return EXIT_FAILURE;
    }

    struct args arguments;
    strcpy(arguments.command, ptoken);
    printf("COMMAND: %s\n", arguments.command);

    int argscount = 0;
    while ( ptoken && argscount < 2 ) {
        ptoken = strtok(NULL, " ");
        if ( ptoken ) {
            strcpy(arguments.args[argscount], ptoken);
            printf("TOKEN: %s\n", arguments.args[argscount++]);
        }
    }

    return 0;
}

输出:

paul@local:~/src/c/scratch$ ./args
test
COMMAND: test

paul@local:~/src/c/scratch$ ./args
test this
COMMAND: test
TOKEN: this

paul@local:~/src/c/scratch$ ./args
test this one
COMMAND: test
TOKEN: this
TOKEN: one

paul@local:~/src/c/scratch$ ./args
test this one two
COMMAND: test
TOKEN: this
TOKEN: one
paul@local:~/src/c/scratch$

我会留下新线作为练习。