1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

ADAMaNT PROGRAM. my text file is somehow being manipulated.

Discussion in 'C' started by jose_peeterson, Aug 15, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    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
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Is this right? (p[ctr]).score or should it be (p[ctr].score)
     
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Why are you using semicolons at the end of following statements :



    Any reason for this? I believe these are the problematic points in your code.

    try this :

    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;
    
     
     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;
    }
     

Share This Page