PDA

View Full Version : problem with strtok & core dump



razza
04-24-2002, 01:21 AM
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;
}
}

razza
04-24-2002, 05:27 AM
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?

razza
04-24-2002, 06:09 AM
nevermind solved it :)
#define STRSIZE 256 just had to be changed to something higher 512 works fine..

Monster
04-24-2002, 09:16 AM
I'm glad you solved it yourself. Here some more tips to prevent core dumps:

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).

razza
04-24-2002, 04:56 PM
Thanks for that, I've only just started to learn how to code so any tips is always appreciated.