maybe this will help...
Code:
#include<stdio.h>
#include<string.h>
struct player{
char name[20];
int score;
};
int read(struct player *p);
void write(struct player *p,int idx);
int main()
{
struct player p[1000];
int idx;
char pname[20];
int same = 1;
int ctr=0;
idx = read(p);
idx = idx + 1;
while(same == 1){
printf("Enter your first name(only) MAROONED player!\n");
scanf("%s",pname);getchar();
while(ctr < idx){ // index has been incremented already
if(strcmp(p[ctr].name,pname)==0){
same = 0;
break;
}
ctr++;
}
if(ctr == (idx - 1)){
strcpy(p[idx].name,pname);
p[idx].score = 0;
break;
}
}
write(p,idx);
system("pause > null");
return 0;
}
int read(struct player *p){
int i=0;
FILE *f;
f = fopen("player.txt","r");
if(f == NULL) {
printf("ERROR : unable to read!\n");
return 0;
}
while(fscanf(f,"%s %d\n",p[i].name,&p[i].score) == 2)
{
i++;
}
fclose(f);
return i;
}
void write(struct player *p,int idx){
FILE *fw;
int ctr;
fw = fopen("player.txt","w");
if(fw == NULL)
{
printf("ERROR opening file!\n");
return;
}
for(ctr=0;ctr<(idx+1);ctr++)
{
fprintf(fw,"%s %d\n",p[ctr].name,p[ctr].score);
}
fclose(fw);
printf("\nplayer records updated\n");
return;
}