Thread: Tokenizing the string problem

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    Tokenizing the string problem

    What I need to do is set num = 2 as it specified in server.conf. At num=atoi(strtok(NULL,"=")); I am getting segfault there or it is giving out 0. I am running linux and gcc 4.1.2.

    server.conf:
    Code:
    num_of_files=2;
    myprog.c:
    Code:
    int c, num;
    char buf[100],buf1[100];
    	
    c=open("./server.conf",O_RDONLY);
    read(c,buf,sizeof buf);
    close(c);
    	
    buf1=strtok(buf,";");
    strtok(buf1,"=");
    num=atoi(strtok(NULL,"="));  //I am getting segfault here or it is giving out 0.
    Last edited by 911help; 12-27-2007 at 06:58 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    read dosn't 0-terminate the string that you read.
    BTW this shouldn't compile. You're trying to assign a pointer to an arry
    Code:
    buf1=strtok(buf,";");
    Kurt

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Sorry my bad it should of been:
    Code:
    char buf[100];
    char *buf1;
    I am not sure why I get segfault or 0 here.
    num=atoi(strtok(NULL,"=")); //I am getting segfault here or it is giving out 0.

    Can someone help. I spent whole day trying figure this out.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by 911help View Post
    I am not sure why I get segfault or 0 here.
    Read this again:

    Quote Originally Posted by ZuK View Post
    read dosn't 0-terminate the string that you read.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    23
    Rather than:
    Code:
    c=open("./server.conf",O_RDONLY);
    read(c,buf,sizeof buf);
    close(c);
    Change it to this:
    Code:
    c=open("./server.conf",O_RDONLY);
    read(c,buf,99);
    close(c);
    buf[99] ="\0";
    Would this fix it?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Why don't you open your while using fopen with the normal text mode and use fgets to read a line from there. Rather than

    Code:
    c=open("./server.conf",O_RDONLY);
    Perhaps doing that way your buf will be terminated by '\0' by fgets itself, reducing your overhead on you explicitly inserting a '\'0 char.

    ssharish

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 911help View Post
    Code:
    buf[99] ="\0";
    Must be:
    Code:
    buf[99] = '\0';
    Else it won't compile. You must learn to differentiate between a string and character. "" = string, '' = character.
    A string is an array of character, so when you assign to an index in the array, you must assign a character, and not a string.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Problem with string tokenizing
    By Mr_Miguel in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 02:02 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM