Thread: problem with strtok & core dump

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Question problem with strtok & core dump

    Im trying to read from /proc/stat the disk_io line, and grab the first number inside the biggest bracket and print it out.

    The following code works ok, if i make another file eg /somedir/robs paste an exact copy of the line in proc stat and remove the "fgets(dummy1, STRSIZE+1, fp);" because on the fake file the disk_io: line im reading is on the first line, where as its partially down the file in /proc/stat, whenever i leave the "fgets(dummy1, STRSIZE+1, fp);" in, gcc compiles the file ok, but it core dumps when i try to run the compiled file.....
    anyone know why?

    #include <stdio.h>
    #include <string.h>
    #define ROBSFILE "/proc/stat"
    #define STRSIZE 256

    //int getIO();
    int getRobs();

    int main()
    {
    if (!getRobs())
    {
    printf("There Is A Problem With getRobs");
    return 1;
    }
    return 0;
    }

    int getRobs()
    {
    FILE *fp;
    char tokenSeperators[] = "(,";
    char dummy1[STRSIZE], robs[STRSIZE];
    char dummy[BUFSIZ], wanted[BUFSIZ];
    if ((fp = fopen(ROBSFILE, "r")) == NULL)
    {
    printf("Can't open file %s\n", ROBSFILE);
    return 0;
    }
    else
    {
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(dummy1, STRSIZE+1, fp);
    fgets(robs, STRSIZE+1, fp);
    fclose(fp);

    strcpy(dummy, strtok(robs, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(dummy, strtok(NULL, tokenSeperators));
    strcpy(wanted, strtok(NULL, tokenSeperators));
    printf("Total Number Of Disk Processes Are %s\n", wanted);

    // printf("TEST: ");
    // printf("%s", robs);
    return 1;
    }
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    I found out it was the line above in the file im reading from that is causing the problems, basically if you use vi to open up the file it goes past the screen and you have to scroll across to see the rest, i backed up the file to another directory/location tested it and it still didnt work (naturally) and then reduced the line till i could see it all without scrolling across and now it works.

    im not sure how to fix it so it doesnt error out when the line is its normal length?

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    nevermind solved it
    #define STRSIZE 256 just had to be changed to something higher 512 works fine..

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I'm glad you solved it yourself. Here some more tips to prevent core dumps:
    Code:
    fgets(dummy1, STRSIZE+1, fp);
    This function reads up to STRSIZE bytes from file and adds a null character to it. So you need a buffer of at least STRSIZE + 1 bytes. Remove the +1 in the fgets function to make it more save. Also check the return value of the fgets function (it can return a NULL pointer).

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    Cool

    Thanks for that, I've only just started to learn how to code so any tips is always appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. why core dump in this function?
    By kocika73 in forum C Programming
    Replies: 7
    Last Post: 02-17-2005, 10:05 PM
  3. core dump
    By kermit in forum Linux Programming
    Replies: 0
    Last Post: 08-03-2004, 06:25 PM
  4. gcc inline asm: illegal instruction (core dump)
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-28-2003, 01:41 PM
  5. core dump by child process
    By anoopks in forum Linux Programming
    Replies: 1
    Last Post: 08-09-2003, 07:35 AM