I'm trying to make a program that reads a file and does stuff with it.
First thing it does is gets some usernames which are contained in the file.
I'll post my code.
Code:
#include <stdio.h>
void sort(int n, int v[], char **name) {
int gap, i, j, temp;
char *temp2;
for(gap = n/2; gap > 0; gap /= 2) {
for(i = gap; i < n; i++) {
for(j = i - gap; j >= 0 && v[j] > v[j+gap]; j -= gap) {
temp = v[j];
temp2 = name[j];
v[j] = v[j+gap];
name[j] = name[j+gap];
v[j+gap] = temp;
name[j+gap] = temp2;
}
}
}
}
int get_ID(char *str, int *ID, char **players) {
char getLine[1024];
FILE *fp;
int i;
char s[20];
int count = 0;
fp = fopen(str, "r");
if(fp == NULL) {
printf("Missing log file\n");
exit(20);
}
while(fgets(getLine, 1024, fp) != NULL) {
if(sscanf(getLine, "%s %x",s,&i)==2) {
if(strcmp(s,"ID:")==0) {
ID[count] = i;
printf("%x\n",i);
fgets(getLine,1024,fp);
sscanf(getLine, "%*s %*s %s",s);
players[count] = s;
printf("%s\n",players[count]);
count += 1;
}
}
if(count==10) {
break;
}
}
return count;
}
int main(int argc, char *argv[]) {
char heroes[10][20];
int ID[10];
char *players[10];
char clicks[20][20];
int i,b;
if(argc<2) {
printf("Usage: fogclicks maplog\n");
exit(10);
}
else {
b = get_ID(argv[1], ID, players);
for(i=0;i<b;i++) {
printf("%s\n",players[i]);
}
sort(b, ID, players);
}
return 0;
}
In the Get_ID function, it prints the player names correctly but then in the main function when i print the player names it just prints the last player name each time in the for loop.
This is my first time posting on a programming forum, sorry if I am not very clear.
Any help appreciated.