CommonTater & anduril462 before you posted your replies I compiled the following program. Which did extracted the data into the three members, but there was a seg fault, which im guessing is something to do with my buffer right?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h> /* strtok() */
#include <unistd.h>
#define BUFFSIZE 200
#define MAXPROCS 1000
struct process {
char *id;
int state;
char *priority;
char *qaunta;
int working;
int waiting;
struct process *next;
};
main (int argc, char **argv)
{
int fd, i;
int status;
char buffer[BUFFSIZE];
char tmpbuff[BUFFSIZE]; /* Temporary buffer to store read lines */
char space[] = " "; /* Space delimeter */
char nl[] = "\n";
char *procstr = NULL; /* String to store processes */
int eof = 0;
int ticks; /* Number of ticks for process */
int ticktime;
status = 99;
fd = open(argv[1], O_RDONLY);
if ( fd == -1 )
{
printf("There was an error opening the file \n");
}
while (!eof)
{
status = read(fd, buffer,sizeof(buffer));
if (status == -1)
printf("There was an error reading the file \n");
if (status > 0)
{
for ( i = 0; i < status; i++)
{
if (buffer[i] == '\n')
{
memcpy(tmpbuff, buffer,BUFFSIZE);
}
}
} else { eof = 1;
}
/* Tokenize 'process string' attributes */
procstr = strtok(tmpbuff,' ');
while ( procstr != NULL ) {
proc[i].id = procstr;
procstr = strtok( NULL, ' ');
printf("%s\n", proc[i].id);
proc[i].qaunta = procstr;
procstr = strtok(NULL , ' ');
printf("%s\n", proc[i].qaunta);
proc[i].priority = procstr;
procstr = strtok( NULL ,'\n');
printf("%s\n", proc[i].priority);
}
}
close(fd);
}
Heres my output:
Process0
7
10
Process1
194 13
Process2
180
2
Process3
47
19
Process4
179
0
Process5
174
16
Process6
171
14
Process7
113
11
Process8
77
14
Process9
15
6
Process10
136
20
Process11
147
4
Process12
50
5
Segmentation fault
Bare in mind there are hundreds of processes that i need to process from each file. Is it possible for me to carry on declaring my two delimeters as they are?
Thanks for any advice