I am reading a file containing structure. I can't seem to print first 14. I am only printing the 15th structure. Can you help?
// ArrayPointerRead.c
#include <stdio.h> /* Function Prototype(s) */
#include <string.h>
#include <stdlib.h>
#define MAX 15
#define NUM 1
struct employee /* Structure Definition */
{
char name[31]; /* Variable Declaration(s) */
char address[45];
char pnumber[10];
};
struct employee *empArray[MAX];
//empArray[MAX] = (struct employee *) malloc(sizeof(struct employee));
void
readFile (struct employee *array[MAX])
{
int i; /* Variable Declaration(s) */
char file_name[20] = {"emp_in.dat"};
FILE * fin;
char name[31]; /* Variable Declaration(s) */
char address[45];
char pnumber[10];
fin = fopen(file_name, "r");
array[MAX] = (struct employee *) malloc(sizeof(struct employee));
printf ("*** Start of reading***\n\n");
for(i = 0; i < MAX; i++) /* Loop for MAX times; 15 names */
{
fgets (name, 32, fin);
fgets (address, 46, fin);
fgets (pnumber, 11, fin);
strcpy (array[MAX]->name, name);
strcpy (array[MAX]->address, address);
strcpy (array[MAX]->pnumber, pnumber);
printf ("%s", array[MAX]->name);
printf ("%s", array[MAX]->address);
printf ("%s\n", array[MAX]->pnumber);
}
fclose(fin);
}
void
printArray (struct employee *empArray[MAX])
{
int i;
for(i = 0; i < MAX; i++) /* Loop for MAX times; 15 names */
{
printf ("%s", empArray[MAX]->name);
// printf ("%s\n", empArray[MAX]->address);
// printf ("%s\n", empArray[MAX]->pnumber);
}
}
void
main(void)
{
// struct employee *empArray[MAX];
readFile(empArray); // passing empArray to read_file ( array )
printArray (empArray);
}
Input file):
Givens, Matthew A.
1432 E. Woodmere Lane
272-0985
Andrew, Marsha L.
12 E. South Blvd.
288-9984
Darrow, Clarence
100 N. Radon Pkwy.
288-3862
Thompson, Cleo M.
360 Circle Circle
277-0001
Childers, Maria
33 Wild Way
288-2777
Wilkinson, George
555 Razor Drive
555-7463
Anderson, Julian R.
6 Sixth Street
661-8676
Brady, Marsha
421 Sickly Sweet Circle
678-1234
McCoy, Kilroy
712 Grafitti Lane
283-9255
Locke, Sandra
818 Prospector Way
223-3445
Michealson, Michael
24908 Centerpark Drive
154-0098
Lear, Chanda
812 Plantation Drive
245-0934
Rounder, Johnathon
287 Jason Drive #32
297-4771
Lincoln, Linda
25 Liberation Plaza
937-3733
Adams, Patricia
25 Carpenter Lane
256-0095