Thanks very kindly for your response but it appears that a demiter of some sort is needed. when i try to read in my data into the array of structures, some of it is truncated. For example, my data is 3000 "Brian Robertson" "Rose Hall" "1784-34567", 1002. When i print the data having stored in an array of sturctures, it looks like this 3000 "Brian Robertson", "Rose 0 > part of the address is missing and the number is replace with 0. However, if I print one line of data, it prints perfectly. this is what my code looks like now.
Code:
#include <stdio.h>
typedef struct{
int id;
char name[30];
char address[50];
char phone[20];
int pid;
}Customer;
int getCustomer(FILE *, Customer[]);
int main ( void )
{
Customer Buyer[1000];
int elements;
int n, j;
static const char filename[] = "c:/users/cyro/desktop/customer.txt";
FILE *file = fopen ( filename, "r" );
if (file == NULL)
{
printf('file cannot open');
exit(1);
}
int i =0;
char line [ 128 ]; /* or other suitable maximum line size */
int numRecords = getCustomer(file, Buyer);
for ( n=0; n <numRecords; n++)printCustomer(Buyer[n]);
printf("\n");
// fclose ( file );
return 0;
}
int getCustomer(FILE * file, Customer list[])
{
int i=0;
char line [150];
int elements;
while (fgets(line, sizeof(line), file) != NULL) /* read a line */
{
//printf("%s",line);
elements = sscanf(line, "%d %s %s %s %d", &list[i].id, list[i].name, list[i].address, list[i].phone, &list[i].pid);
++i;
}
return i;
}
void printCustomer(Customer c)
{
printf("%d %s %s %s %d\n",c.id, c.name, c.address, c.phone, c.pid);
}
1002 "Brian Robertson" "Rose Hall" "567-45455" 101
1003 "Zeth Robertson" "Spring Village" "456-90987" 102
1004 "Nichle Morris" "Rose Bank" "345-34233" 103
1005 "Antonio Robertson" "Troumaca" "345-45455" 104