Why are you using semicolons at the end of following statements :
Quote:
if(strcmp(((p[ctr]).name),pname) == 0);
Quote:
while(fscanf(fr,"%s%d\n",((p[i]).name),&((p[i]).score)) == 2);
Any reason for this? I believe these are the problematic points in your code.
try this :
Code: c
#include<stdio.h>
#include<string.h>
struct player
{
char name[20];
int score;
};
int read(struct player p[1000]);
void write(struct player p[1000],int idx);
int main()
{
struct player p[1000];
//int size;
int idx;
char pname[20];
int same = 1;
int ctr;
idx = read(p);
// size = idx;
while(same == 1)
{
printf("Enter your first name(only) MAROONED player!\n");
scanf("%s",pname);
ctr = 0;
while(ctr < idx)
{
if(strcmp(((p[ctr]).name),pname) == 0)
{
same = 0;
break;
}
ctr++;
}
printf(" record not found!!! ctr = %d and idx = %d\n", ctr, idx);
if(ctr == idx)
{
strcpy(((p[idx]).name),pname);
((p[idx]).score) = 0;
idx++;
break;
}
}
printf("\n%s %s\n",p[1].name,p[2].name);
write(p,idx);
system("pause > null");
return 0;
}
int read(struct player p[1000])
{
int i=0;
FILE *fr;
fr = fopen("player.txt","r");
if(fr == NULL)
{
printf("ERROR : unable to read!\n");
return 0;
}
while(fscanf(fr,"%s%d\n",((p[i]).name),&((p[i]).score)) == 2)
{
printf(" \nplayer Name = %s with score = %d\n", p[i].name, p[i].score);
i++;
}
printf("Final val of idx = %d\n", i);
fclose(fr);
return i;
}
void write(struct player p[1000],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);ctr++)
{
fprintf(fw,"%s %d\n",((p[ctr]).name),((p[ctr]).score));
}
fclose(fw);
printf("\nplayer records updated\n");
return;
}