strtok导致分段错误 [英] strtok causes Segmentation fault

查看:97
本文介绍了strtok导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分隔一个字符串。分隔符是分号和逗号。

当我运行程序时,我在第一个strtok上出现了分段错误。

我按照其他人的例子和我的旧C书,但我不能...... b $ b似乎找到了问题。访问列表的格式为

20,45; 22,44; 46,28; 99,43等。我做错了什么?

谢谢,


#include< sys / signal.h>


#include< messages.h>

#include< stdio.h>

#include< string.h>

#include < stdlib.h>

#include< unistd.h>

#include< ctype.h>


#define或||

#define和&&


int createvarible(const int sock,char * name,char * type,char * value ,

char * list)

{

int n = 0,size,returnval = 0;

char * tokenptr = NULL,* tokenptr2 = NULL;

int accesscode,portnum;


tokenptr = strtok(list," ;;"); //这一行会导致seg错误为什么?


if(tokenptr == NULL){

tokenptr2 = strtok(list,",") ; //分隔访问码,因为没有

分号

accesscode = atoi(tokenptr2);

tokenptr2 = strtok(NULL,", "); //分隔端口

portnum = atoi(tokenptr2);

printf(" code [0]%d \ n",accesscode);

printf(" port [0]%d \ n",portnum); }

else {

while(tokenptr!= NULL){//不要担心这里的代码吧

做什么我需要

tokenptr2 = strtok(list," ;;"); //分隔访问码

accesscode = atoi(tokenptr2);

tokenptr2 = strtok(NULL,","); // delimit portnum

portnum = atoi(tokenptr2);

printf(" code [0]%d \ n",accesscode);

printf(" port [0]%d \ n",portnum);

n ++;

}

}

返回访问码;

}

解决方案

bo******@hotmail.com 写道:

我需要分隔一个字符串。分隔符是分号和逗号。
当我运行程序时,我在第一个strtok上遇到了分段错误。
我按照其他人的例子和旧C书的例子,但我不能
似乎找到了问题。访问列表的格式为
20,45; 22,44; 46,28; 99,43等。我做错了什么?
谢谢,
#include< sys / signal.h>

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

#define或||
#define和&

int createvarible(const int sock,char * name,char * type,char * value,
char * list)
{n / 0,size,returnval = 0;
char * tokenptr = NULL,* tokenptr2 = NULL;
int accesscode,portnum;

tokenptr = strtok(list," ;;"); //这一行会导致seg错误为什么?



首次用作strtok()的arg时,列表是非空的吗?它是

是否空终止?




" void * clvrmnky()" < CL ************** @ hotmail.com.invalid>在留言中写道

新闻:KE ****************** @ nnrp.ca.mci.com!nnrp1.uu net.ca ...

bo******@hotmail.com 写道:

我需要分隔一个字符串。分隔符是分号和逗号。
当我运行程序时,我在第一个strtok上遇到了分段错误。
我按照其他人的例子和旧C书的例子,但我不能
似乎找到了问题。访问列表的格式为
20,45; 22,44; 46,28; 99,43等。我做错了什么?
谢谢,
#include< sys / signal.h>

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

#define或||
#define和&

int createvarible(const int sock,char * name,char * type,char * value,
char * list)
{n / 0,size,returnval = 0;
char * tokenptr = NULL,* tokenptr2 = NULL;
int accesscode,portnum;

tokenptr = strtok(list," ;;"); //这一行会导致seg错误为什么?


当第一个用作stgok()的arg时,列表是非空的吗?它是否以空终止?




非标准的东西。这个问题是否已经被框架化,以便在clc中成为主题? Joe


以下是我调用createvarible的方法:

createvarible(ss," xyz"," string","这是一个test,

" 22,43; 44,33")

所以为了回答你的问题,字符串为空终止。要null

终止字符串我认为我必须把\0放在最右边?


谢谢,


I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can''t
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

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

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?

if (tokenptr == NULL) {
tokenptr2 = strtok(list, ",");//delimit accesscode as there is no
semicolon
accesscode=atoi(tokenptr2);
tokenptr2 = strtok(NULL, ",");//delimit port
portnum=atoi(tokenptr2);
printf("code[0] %d\n", accesscode);
printf("port[0] %d\n", portnum); }
else {
while (tokenptr != NULL) { //don''t worry about the code down here it
does what I need
tokenptr2 = strtok(list, ";");//delimit accesscode
accesscode=atoi(tokenptr2);
tokenptr2 = strtok(NULL, ",");//delimit portnum
portnum=atoi(tokenptr2);
printf("code[0] %d\n",accesscode);
printf("port[0] %d\n",portnum);
n++;
}
}
return accesscode;
}

解决方案

bo******@hotmail.com wrote:

I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can''t
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

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

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?


Is list non-null when first used as an arg to strtok()? Is it
null-terminated?



"void * clvrmnky()" <cl**************@hotmail.com.invalid> wrote in message
news:KE******************@nnrp.ca.mci.com!nnrp1.uu net.ca...

bo******@hotmail.com wrote:

I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can''t
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,

#include <sys/signal.h>

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

#define or ||
#define and &&

int createvarible(const int sock, char *name, char *type, char *value,
char *list)
{
int n=0, size, returnval=0;
char *tokenptr=NULL, *tokenptr2=NULL;
int accesscode, portnum;

tokenptr = strtok(list, ";"); //this line causes the seg fault WHY?


Is list non-null when first used as an arg to strtok()? Is it
null-terminated?



Non Standard stuff. Could this question have been framed so as to be
topical in clc? Joe


Here is how I call createvarible:
createvarible(ss, "xyz", "string", "this is a test",
"22,43;44,33")
So to answer your question the string is null terminated. To null
terminate the string I think I have to put \0 at the end right?

Thanks,


这篇关于strtok导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆