matt
2010-01-18 08:17:35 UTC
#include
#include
#include
#include
#include
#include
struct content
{
char *pro_name;
int priority;
int quanta;
}ps[2000];
main (int argc, char **argv)
{
int status;
struct stat inode;
int fd;
char *buffer;
int i = 0;
char *saveptr1;
char *saveptr2;
char *line;
int n = 0;
/* Check the command line arguments */
if (argc != 2)
{
printf("syntax is: %s filename\n", argv[0]);
exit (1);
}
/* Check the name exists and is a file */
if ((stat(argv[1], &inode) == -1) || (!S_ISREG(inode.st_mode)))
{
printf("%s is not a file\n", argv[1]);
exit(2);
}
/* Open the file for reading */
fd = open(argv[1], O_RDONLY, 0);
if (fd == -1)
{
printf("%s cannot be opened\n", argv[1]);
exit(3);
}
buffer=(char*)malloc(inode.st_size * sizeof(char) + 1);
buffer[inode.st_size+1] = '\0';
/* Begin processing the file here */
status = read(fd, buffer, inode.st_size);
if (status == -1)
{
printf("%s cannot be read\n", argv[1]);
exit(4);
}
printf("\ninode size:%d | status size:%d\n\n",(int)inode.st_size, status);
/* extract first string from string sequence */
line = strtok_r(buffer, "\n",&saveptr1);
/* loop until finishied */
while (line !=NULL)
{
/* extract string from string sequence */
ps[i].pro_name = strtok_r(line, " ",&saveptr2);
ps[i].quanta = atoi(strtok_r(NULL, " ",&saveptr2));
ps[i].priority = atoi(strtok_r(NULL," ", &saveptr2));
/* print string after tokenized */
printf("name: %s\nquanta: %d\npriority: %d\n", ps[i].pro_name, ps[i].quanta, ps[i].priority);
line = strtok_r(NULL,"\n",&saveptr1);
i++;
}
/* test to see if array worked to access individual content */
printf("\nProcess Name: %s --- its Quanta (CPU Time) is: %d\n\n", ps[4].pro_name, ps[4].quanta);
close(fd);
free(buffer);
}
Now i need to find a way of sorting the contents of the file i read in.
the file i read in has this content :
Line01 5 7
Line02 6 1
LIne03 1 3
i have split it so Line01 goes into pro_name, then the number after "5" goes into quanta then "7" goes into priority. now i need to find a way of arranging the contents of quanta into ascending order and then do the same with priority.... have looked at using bubble sort but i have had a few goes and i have no how idea how i will do it with the array of structs
any help would be appreciated
thank you