Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- char ** readFile(int fd);
- int * split_grades(char * stri);
- char * split_name(char * stri);
- double average(int * nr);
- int myCompare(const void*a, const void *b);
- int main(int argc, char** argv)
- {
- if(argc!=2){
- printf("Enter one argument which is the name of the file\n");
- return 0;
- }
- char *buf;
- int bytesRead;
- int fp;
- //printf("%s\n",argv[1]);
- fp=open(argv[1], O_RDONLY);
- char **students=readFile(fp);
- int i,pid2,n;
- printf("Students are: \n");
- n=atoi(students[0]);
- for(i=1; i<n; i++)
- printf("%s",*(students+i));
- //printf("%d\n",n);
- int pid=fork();
- if(pid==0)
- {
- printf("Averages:\n");
- for(i=1; i<n; i++)
- printf("%s average [%d]: %f\n",split_name(*(students+i)),i,average(split_grades(*(students+i))));
- }
- else
- {
- pid2=fork();
- if(pid2==0)
- {
- printf("Ordered:\n");
- qsort(students+1,n-1,sizeof(char*),myCompare);
- int j;
- for(j=1; j<n; j++)
- printf("Ord [%d]: %s",j,*(students+j));
- }
- }
- close(fp);
- return 0;
- }
- double average(int * nr)
- {
- int i;
- int sum=0;
- for(i=1; i<*(nr+0); i++)
- sum+=*(nr+i);
- return sum*1.0/((*(nr+0)-1)*1.0);
- }
- int * split_grades(char * stri)
- {
- char * string=(char*)malloc(sizeof(char)*(strlen(stri)+1));
- strcpy(string,stri);
- char * pch;
- int *numbers=(int*) malloc(sizeof(int)*2);
- char nr_count=1;
- char *splitted_string;
- splitted_string=(char*)malloc(sizeof(char)*(strlen(string)+1));
- pch = strtok (string," ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
- while (pch != NULL)
- {
- numbers[nr_count++]=atoi(pch);
- numbers=(int*)realloc(numbers, (nr_count+1)*sizeof(int));
- numbers[0]=nr_count;
- pch = strtok (NULL, " ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
- }
- return numbers;
- }
- char * split_name(char * stri)
- {
- char * string=(char*)malloc(sizeof(char)*(strlen(stri)+1));
- strcpy(string,stri);
- char * pch;
- char *splitted_string;
- int n;
- splitted_string=(char*)malloc(sizeof(char)*(strlen(string)+1));
- pch = strtok (string," ,0123456789\n");
- while (pch != NULL)
- {
- strcat(splitted_string,pch);
- n=strlen(splitted_string);
- if(pch = strtok (NULL, " ,0123456879\n"))
- {
- splitted_string[n]=' ';
- splitted_string[n+1]='\0';
- }
- //pch = strtok (NULL, " ,0123456879\n");
- }
- return splitted_string;
- }
- char ** readFile(int fd)
- {
- char **student_grades=(char**)malloc(sizeof(char*)*2);
- int nr_count=1;
- char buffer[1000];
- int bytes_read;
- int k = 0;
- char chr;
- *(student_grades+0)=(char*)malloc(sizeof(char*)*11);
- while((bytes_read=read(fd,&chr,1))>0)
- {
- // bytes_read = read(fd, &chr, 1);
- buffer[k++]=chr;
- if(chr == '\n' || chr == '\0')
- {
- buffer[k]='\0';
- *(student_grades+nr_count)=(char*)malloc(sizeof(char)*(strlen(buffer)+1));
- strcpy(student_grades[nr_count++],buffer);
- student_grades=(char**)realloc(student_grades, (nr_count+1)*sizeof(char*));
- sprintf(*(student_grades+0), "%d", nr_count);
- memset(buffer,0,k);
- k = 0;
- }
- }
- return student_grades;
- }
- int myCompare (const void * a, const void * b )
- {
- const char **ia = (const char **)a;
- const char **ib = (const char **)b;
- return strcmp(*ia, *ib);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement