ADAMaNT PROGRAM. my text file is somehow being manipulated.

jose_peeterson's Avatar, Join Date: May 2011
Contributor
dear experts,
please try running the following record keeping program. the text file is manipulated wrongly at the end of every execution cant seem to find the error. pls help if possible

Code:
#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 + 1;
 
 while(same == 1)
  {
   printf("Enter your first name(only) MAROONED player!\n");
   scanf("%s",pname);
   
   ctr = 0;
   while(ctr < size) 
    {
     if(strcmp(((p[ctr]).name),pname) == 0);
      { 
       same = 0;
       break;
      }
     ctr++;
    }
   if(ctr == idx)
    {
     strcpy(((p[size]).name),pname);
     ((p[idx]).score) = 0;
     break;
    }
  }

printf("\n%s %s\n",p[1].name,p[2].name);
 write(p,size);
 
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);        
   {
    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+1);ctr++)
  {
   fprintf(fw,"%s %d\n",((p[ctr]).name),((p[ctr]).score));
  }

fclose(fw);

printf("\nplayer records updated\n");

return;
}


the player.txt text file initially has

Code:
john 1
peeter 2
mary 8
text file after execution

Code:
mary 8
 0
 0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Is this right? (p[ctr]).score or should it be (p[ctr].score)
poornaMoksha's Avatar, Join Date: Jan 2011
Ambitious contributor
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;
}