Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #define BUFFSIZE 1024
- int n;
- int file;
- char buff[BUFFSIZE];
- struct student{
- char *name;
- int grades[5];
- } students[100];
- int size=0;
- int main()
- {
- if((file = open("file.txt",O_RDONLY)) <0)
- {
- printf("ERROR OPENING THE FILE ");
- return 1;
- }
- while((n = read(file,buff,BUFFSIZE)))
- {
- char newBuff[BUFFSIZE];
- strcpy(newBuff,buff);
- char *p = strtok(newBuff,":,.\n");
- int index = 0;
- while(p)
- {
- //printf("%s\n",p);
- if(index== 0)
- {
- students[size].name = malloc(sizeof(char) * (strlen(p)+1));
- strcpy(students[size].name,p);
- ++index;
- }
- else
- {
- int grade = atoi(p);
- // printf("%d\n",grade);
- students[size].grades[index-1] =grade;
- ++index;
- }
- if(index == 6)
- {
- index = 0;
- size++;
- }
- p = strtok(NULL,":,.\n");
- }
- }
- pid_t pid = fork();
- if(pid < 0)
- {
- printf("ERROR WHILE FORKING");
- return -1;
- }
- else if(pid == 0)
- {
- ///CHILD
- for(int i=0;i<size;i++)
- {
- double avg = 0;
- for(int j=0;j<5;j++)
- {
- avg += students[i].grades[j];
- }
- avg = avg /5;
- printf("The stundet: %s has the avg: %f",students[i].name,avg);
- }
- }
- else
- {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement