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