/* A little football team league table program */ /*required libs*/ #include #include /*A team structure*/ typedef struct team { char name[31]; int games, points, goalsFor, goalsAgainst; } Team; /*Protocole*/ Team **getnewteamArray(int noOfTeams, FILE *teamsFile); void *xmalloc(size_t n); void addNewTeam(Team **teamArray,int noOfTeams); int main () { int menOption=0, noOfTeams=0; char name[31]; int games, points, goalsFor, goalsAgainst; Team **teamArray = NULL; FILE *teamsFile; teamsFile = fopen("./teams", "rw"); if (teamsFile == NULL) { printf("File could not be opened\n"); exit(5); } while (fscanf(teamsFile, "%s%i%i%i%i",&name, &games, &points, &goalsFor, &goalsAgainst) != EOF) noOfTeams++; printf("\n\n%i\n\n",noOfTeams); if (noOfTeams == 0) { fprintf(stdout,"There are currently no teams in the system, please add a new one\n"); addNewTeam(teamArray,noOfTeams); noOfTeams++; } else { teamArray = getnewteamArray(noOfTeams, teamsFile); } while ((menOption != 5) && (menOption != 6)) { do { fprintf(stdout,"Please enter the number of the option you would like to use:\n"); fprintf(stdout,"1.\tAdd a new team.\n"); fprintf(stdout,"2.\tDelete a team.\n"); fprintf(stdout,"3.\tAdd a result.\n"); fprintf(stdout,"4.\tPrint a league table.\n"); fprintf(stdout,"5.\tExit without saving.\n"); fprintf(stdout,"6.\tSave and exit.\n"); scanf("%i", &menOption); } while ((menOption <= 0) || (menOption > 6)); if (menOption == 1) { printf("%i", noOfTeams); addNewTeam(teamArray,noOfTeams); noOfTeams++; } } } void addNewTeam(Team **teamArray,int noOfTeams) { noOfTeams++; Team **tmp; if ((tmp = realloc(teamArray, noOfTeams * sizeof *tmp)) == NULL) { /* Possible free on ip? Depends on what you want */ fprintf(stderr, "ERROR: realloc failed"); } else { printf("\n\n\n\n\n\n\tet\n\n\n\n\n\n"); teamArray = tmp; } teamArray[noOfTeams - 1] = xmalloc(sizeof *teamArray[noOfTeams - 1]); do { fprintf(stdout, "Enter the team name:"); scanf("%s", &teamArray[noOfTeams -1]->name); } while (teamArray[noOfTeams -1]->name == 0); do { fprintf(stdout, "Enter the number of games played:"); scanf("%i", &teamArray[noOfTeams -1]->games); } while (teamArray[noOfTeams -1]->games < 0); do { fprintf(stdout, "Enter the number of points:"); scanf("%i", &teamArray[noOfTeams -1]->points); } while (teamArray[noOfTeams -1]->points < 0); do { fprintf(stdout, "Enter the number of goals for:"); scanf("%i", &teamArray[noOfTeams -1]->goalsFor); } while (teamArray[noOfTeams -1]->goalsFor < 0); do { fprintf(stdout, "Enter the number of goals against:"); scanf("%i", &teamArray[noOfTeams -1]->goalsAgainst); } while (teamArray[noOfTeams -1]->goalsAgainst < 0); fprintf(stdout, "%s %i %i %i %i\n", teamArray[noOfTeams -1]->name, teamArray[noOfTeams -1]->games, teamArray[noOfTeams -1]->points, teamArray[noOfTeams -1]->goalsFor, teamArray[noOfTeams -1]->goalsAgainst); } Team **getnewteamArray(int noOfTeams, FILE *teamsFile) { Team **teamArray = NULL; char name[31]; int games, points, goalsFor, goalsAgainst, i=0; rewind(teamsFile); teamArray = xmalloc(noOfTeams * sizeof *teamArray); for (i=0;iname, &teamArray[i]->games, &teamArray[i]->points, &teamArray[i]->goalsFor, &teamArray[i]->goalsAgainst); fprintf(stdout, "%s %i %i %i %i\n", teamArray[i]->name, teamArray[i]->games, teamArray[i]->points, teamArray[i]->goalsFor, teamArray[i]->goalsAgainst); } if (noOfTeams == 0) { fprintf(stdout,"There are currently no teams in the system, please add a new one\n"); } else { } //teamArray = xmalloc(); return teamArray; } void *xmalloc(size_t n) { void *p = malloc(n); if (p == NULL) { fprintf(stderr, "Out of memory!\n"); exit(-1); } } ed@dangermouse:~/General/Uni/c2/week7$ ./code 3 test1 1 2 3 4 test2 1 2 3 4 test3 1 2 3 4 Please enter the number of the option you would like to use: 1. Add a new team. 2. Delete a team. 3. Add a result. 4. Print a league table. 5. Exit without saving. 6. Save and exit. 1 3 et Enter the team name:test231 Enter the number of games played:2 Enter the number of points:3 Enter the number of goals for:3 Enter the number of goals against:2 test231 2 3 3 2 Please enter the number of the option you would like to use: 1. Add a new team. 2. Delete a team. 3. Add a result. 4. Print a league table. 5. Exit without saving. 6. Save and exit. 1 *** glibc detected *** ./code: double free or corruption (fasttop): 0x0804b170 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6[0xb7e648bd] /lib/tls/i686/cmov/libc.so.6[0xb7e66194] /lib/tls/i686/cmov/libc.so.6(__libc_realloc+0xff)[0xb7e66cdf] ./code[0x804894a] ./code[0x80488f6] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb7e138cc] ./code[0x8048601] ======= Memory map: ======== 08048000-0804a000 r-xp 00000000 08:16 1998884 /home/ed/General/Uni/c2/week7/code 0804a000-0804b000 rw-p 00001000 08:16 1998884 /home/ed/General/Uni/c2/week7/code 0804b000-0806c000 rw-p 0804b000 00:00 0 [heap] b7c00000-b7c21000 rw-p b7c00000 00:00 0 b7c21000-b7d00000 ---p b7c21000 00:00 0 b7de7000-b7df1000 r-xp 00000000 08:15 1966147 /lib/libgcc_s.so.1 b7df1000-b7df2000 rw-p 00009000 08:15 1966147 /lib/libgcc_s.so.1 b7dfd000-b7dfe000 rw-p b7dfd000 00:00 0 b7dfe000-b7f2b000 r-xp 00000000 08:15 1998885 /lib/tls/i686/cmov/libc-2.4.so b7f2b000-b7f2d000 r--p 0012c000 08:15 1998885 /lib/tls/i686/cmov/libc-2.4.so b7f2d000-b7f2f000 rw-p 0012e000 08:15 1998885 /lib/tls/i686/cmov/libc-2.4.so b7f2f000-b7f32000 rw-p b7f2f000 00:00 0 b7f32000-b7f56000 r-xp 00000000 08:15 1998893 /lib/tls/i686/cmov/libm-2.4.so b7f56000-b7f58000 rw-p 00023000 08:15 1998893 /lib/tls/i686/cmov/libm-2.4.so b7f60000-b7f65000 rw-p b7f60000 00:00 0 b7f65000-b7f7e000 r-xp 00000000 08:15 1966102 /lib/ld-2.4.so b7f7e000-b7f80000 rw-p 00018000 08:15 1966102 /lib/ld-2.4.so bfc87000-bfc9c000 rw-p bfc87000 00:00 0 [stack] ffffe000-fffff000 ---p 00000000 00:00 0 [vdso] 4Aborted (core dumped)