I have a function getData() where it is supposed to read a text file "Users.txt" and store the data of the text file in an array of structures declared as user[20].
the struct is defined as:
typedef struct userTag {
int ID;
string10 password;
string20 name;
string30 address;
string15 contactNum;
} userType;
the text file format is as follows:
<ID> <password>
<name>
<address>
<contact num>
112 pass222
Ginge Akerwood
Camella Homes
0922294812
119 h3yo2
Marian Nilza Ginge
Camella
0999222444
this is my main program:
#include <stdio.h>
#include <stdlib.h>
#include "Data.h"
int main(int argc, char *argv[]) {
userType user[20];
getData(user);
display(user, 3);
return 0;
}
void display(userType u[], int nElem) {
int i;
for(i = 0; i < nElem; i++)
printf("User ID: %d\n", u[i].ID);
}
void getData(userType u[]) {
int i = 0;
FILE* fp_users;
char dump;
fp_users = fopen("Users.txt", "r");
while(fscanf(fp_users,"%d %s", &u[i].ID, u[i].password) == 2) {
fgets(u[i].name, 20, fp_users);
fgets(u[i].address, 30, fp_users);
fgets(u[i].contactNum, 15, fp_users);
fscanf(fp_users, "%c", &dump);
i++;
}
fclose(fp_users);
}
the output of the function display() is:
User ID: 112
User ID: 922294812
User ID: 999222444
I can't really find good resources on how to read files on the net so i had to ask here. also the format of the text file can't be changed so it has to be read that way.