Here is code snippet where I read values from a file and createClient from that data. I have an array of Clients and I am giving different values to different elements of array.
FILE *clientFile;
clientFile = fopen("clients.txt", "r");
char id[256];
char name[256];
char phone[256];
char email[256];
Client cptrs[10];
int i=0;
while(fgets(id, sizeof(id), clientFile)){
//fscanf(clientFile, "%s", name);
fgets(name, sizeof(name), clientFile);
fgets(phone, sizeof(phone), clientFile);
fgets(email, sizeof(email), clientFile);
/*fscanf(clientFile, "%s", phone);
fscanf(clientFile, "%s", email);*/
//printf("%s %s %s %s\n", id,name,phone,email);
cptrs[i] = createClient(id,name,phone,email);
//printc(cptrs[i]);
//printf("%d\n", i);
i++;
}
printc(cptrs[0]);
printc(cptrs[1]);
printc(cptrs[2]);
All 3 print functions output the same result which is the last data in the file.
Here are struct client, createClient method and printc method. I have included both client.h and client.c files.
client.h
#ifndef CLIENT_H
#define CLIENT_H
typedef struct client *Client;
Client createClient(char* id, char* name, char* phone, char* email);
void destroyClient(Client cP);
void printc(Client cP);
#endif
client.c
#include "client.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct client {
char* id;
char* name;
char* phone;
char* email;
};
// returns the pointer to the list; NULL if list not created
Client createClient(char* id, char* name, char* phone, char* email) {
// allocate memory for a structure variable containing all
// list components
Client cptr = malloc(sizeof(struct client));
// if allocation was succesfull
if (cptr != NULL) {
cptr->id = id;
cptr->name = name;
cptr->phone = phone;
cptr->email = email;
}
return cptr;
}
void destroyClient(Client cptr) {
free(cptr);
}
void printc(Client cptr){
printf("%s %s %s %s\n", cptr->id, cptr->name, cptr->phone, cptr->email);
}
Here is clients.txt file
1212
Joseph Miller
206-555-1212
millers@comcast.net
1313
Beatrice Pizarro Ozuna
206-111-1111
bea@uw.edu
1314
Anne Simpson
425-777-8888
a.simpson@gmail.com
1100
Emily Price
206-111-5555
priceless@yahoo.com
1289
Sharon Henderson
206-555-1289
henderson21@comcast.net
1316
Sylvia Williamson
425-123-8888
sylvia@gmail.com
1101
Michael Murphy
425-111-5555
gemini@yahoo.com
The output of the first code is:
1101
Michael Murphy
425-111-5555
gemini@yahoo.com
1101
Michael Murphy
425-111-5555
gemini@yahoo.com
1101
Michael Murphy
425-111-5555
gemini@yahoo.com
I don't understand why all array elements store the same element (the last one in the file). I want them to store respective elements. Please help.