I am trying to create a simple phone book program that reads data from a file and saves the contents to specific nodes in a list. It works fine if I use the addEntry function with static data, for example:
addEntry("First", "Last", "555-555-5555");
If I try to read more than 1 record from a file, each record will look like the last record in the file. For example, if my file contains:
First1
Last1
123-456-7890
First2
Last2
987-654-3210
After saving the data in the list and printing, the result will look like this:
First2
Last2
987-654-3210
First2
Last2
987-654-3210
Instead of printing each specific name and number. This bothers me because this problem only occurs when I am reading data from a file, and not when I manually enter the name and number in the function call. Here are the definitions for main and addEntry, thanks in advance.
#include <stdlib.h>
#include <stdio.h>
struct bookNode
{
char * firstName;
char * lastName;
char * phoneNumber;
struct bookNode * next;
} * head;
FILE * fpointer;
void addEntry(char * fn, char * ln, char * pn);
void display();
int numEntries();
void writeBookData(struct bookNode * selection);
int main()
{
head = NULL;
addEntry("Test", "Name", "111-111-1111");
addEntry("Test2", "Name2", "222-222-2222");
int i;
fpointer = fopen("addressbook.dat", "a+");
if(fpointer == NULL)
{
printf("Error: addressbook.dat could not be opened.\n");
}
char first[20];
char last[20];
char num[20];
while (!feof(fpointer))
{
fgets(first, 20, fpointer);
fgets(last, 20, fpointer);
fgets(num, 20, fpointer);
i = 0;
while(first[i] != '\n')
{
i++;
}
first[i] = '\0';
i = 0;
while(last[i] != '\n')
{
i++;
}
last[i] = '\0';
addEntry(first, last, num);
}
fclose(fpointer);
display();
int entryCount = numEntries();
printf("There are %d entries in this Address Book\n", entryCount);
return EXIT_SUCCESS;
}
void addEntry(char * fn, char * ln, char * pn)
{
struct bookNode * tempNode, * iterator;
tempNode = (struct bookNode *)malloc(sizeof(struct bookNode));
tempNode->firstName = fn;
tempNode->lastName = ln;
tempNode->phoneNumber = pn;
iterator = head;
if (head == NULL)
{
head = tempNode;
head->next = NULL;
}
else
{
while(iterator->next != NULL)
{
iterator = iterator->next;
}
tempNode->next = NULL;
iterator->next = tempNode;
}
}