0

What i am trying to do is reading information from text file and store it in struct person.

I tried to store multiple persons information in array of struct. My code works for integer values but every persons name is the same, somehow all the persons name is the last persons name. I am new on c so dont know exactly how this memory works but if i print my array in while loop everything seems fine but if i try to print array outside of assignment loop names reamins same.

This is my Person struct:

typedef enum Bool{false, true}boolean;

struct PERSON{
    char *name;
    float money;
    float betAmount;
    int betNumber;

boolean (*isWin)(struct KISI*,int);
void (*delete)(struct KISI*);
};
typedef struct PERSON* Person;
Person new_Kisi(char*,float,float,int);
void delete_Kisi(const Person);

This is the Person.c:

Person new_Kisi(char* name,float money,float betAmount, int betNumber)
{
    Person this;
    this = (Person)malloc(sizeof(struct PERSON));
    this->name=name;
    this->money = money;
    this->betAmount = betAmount;
    this-> betNumber = betNumber;
    this->delete = &delete_Kisi;
    return this;

}

void delete_Kisi(const Person this){
    if(this==NULL) return;
    free(this);
}

This is my code so far:

FILE* fp;
    char buffer_in[256];
    fp=fopen("Kisiler.txt", "r");
    if (fp==NULL)
    {
        return 1;
    }
    
    int count_lines = 0;
    char chr;
    
    chr = getc(fp);
    while (chr != EOF)
    {
        //Count whenever new line is encountered
        if (chr == 'n')
        {
            count_lines = count_lines + 1;
        }
        //take next character from file.
        chr = getc(fp);
    }
    fclose(fp);
    fp=fopen("Kisiler.txt", "r");
    int personNum=0;
        Person persons[count_lines];
        memset(persons,0,sizeof(struct KISI)*count_lines);
        char *namePerson;
        char temp[64]={0};
        float money;
        float betAmount;
        int betNumber;

while (!feof(fp))
{
    fgets(buffer_in, 256 , fp);
    sscanf(buffer_in, "%[a-zA-Z ]#%f#%f#%d",temp,&money,&betAmount,&betNumber);
    namePerson=&temp[0];
    persons[personNum]=new_Kisi(namePerson,money,betAmount,betNumber);
    
    printf("%s %f %.2f %d \n", 
        persons[personNum]->name, persons[personNum]->money, persons[personNum]->betAmount, persons[personNum]->betNumber);

    personNum++;
}
printf("\n<<<<<<<<<<<<<<<<<<<< after while loop >>>>>>>>>>> \n");

fclose(fp);
int n=personNum;
for (int i = 0; i <n; i++)
{  
    printf("%s \n", 
        persons[i]->name);
    
}

This is the output:

<<<<<<<<<<<<<<<<< inside of while loop >>>>>>>>>>>
    Rafaela Carroll 79289.710938 0.89 1
    Donette Wilkinson 29622.169922 0.95 8
    Hong Senger 99735.367188 0.59 1
    Kim Dibbert 64012.718750 0.35 6
    Ruben Ferry 54201.261719 0.10 1
    
<<<<<<<<<<<<<<<<<<<< outside of while loop >>>>>>>>>>>
Ruben Ferry 79289.710938 0.89 1
Ruben Ferry 29622.169922 0.95 8
Ruben Ferry 99735.367188 0.59 1
Ruben Ferry 64012.718750 0.35 6
Ruben Ferry 54201.261719 0.10 1
  • 1
    Each of your created structures gets a pointer to the *same* buffer, with `this->name=name;`. You can't copy strings like that. Something like `this->name=strdup(name);` would work, but remember to free that allocated space when you destroy each object. [This should be a duplicate ...] – Adrian Mole May 07 '22 at 16:23
  • 1
    ... I'm still looking for a suitable question to make a canonical Q/A for this issue. However, this *isn't* that question, because there are too many other problems in the code. For example: [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/10871073) and the `this` argument in the `delete_Kisi` function isn't a pointer. – Adrian Mole May 07 '22 at 16:29
  • See also [Is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/) — TL;DR, generally the answer is "No", with pointers to functions and perhaps pointers to opaque types being exceptions. – Jonathan Leffler May 07 '22 at 17:17

0 Answers0