B
bofh1234
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;
}
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;
}