Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 570523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:24:38+00:00 2026-05-13T13:24:38+00:00

I am trying to create a simple phonebook program that reads data from a

  • 0

I am trying to create a simple phonebook program that reads data from a file and stores the content into specific nodes in the list. It works fine if I use my addEntry function with static data, such as:

addEntry("First", "Last", "555-555-5555");

If I try to read more than 1 entry from the file, each entry just appears to be whatever the last entry was in the file. For example, if my file contained:

First1
Last1
123-456-7890
First2
Last2
987-654-3210

After storing the data in the list and printing, the output would look like:

First2
Last2
987-654-3210

First2
Last2
987-654-3210

Rather than printing each specific name and number. This confuses me because this issue only occurs when I read data from the file, not when I manually type in the name and number in a function call. Here are the definitions for main and addEntry, thank you 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"); // These entries will work as intended

    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);

        //Removes newline characters from the ends of the names
        i = 0;
        while(first[i] != '\n')
        {
            i++;
        }
        first[i] = '\0';
        i = 0;
        while(last[i] != '\n')
        {
             i++;
        }
        last[i] = '\0';

        // Adds the entry from the strings with the file data in them
        addEntry(first, last, num);
    }
    fclose(fpointer);

    display(); // typical linked list display function

    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 the list is empty
    if (head == NULL)
    {
        head = tempNode;
        head->next = NULL;
    }

    // The list is not empty
    else
    {
        while(iterator->next != NULL)
        {
            iterator = iterator->next;
        }
        tempNode->next = NULL;
        iterator->next = tempNode;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:24:38+00:00Added an answer on May 13, 2026 at 1:24 pm

    You need to copy the string values to each new node. You are only storing the pointer to each string, but it is always the same pointers (first, last and num that are declared in main) so they all point to the same memory.

    So in your addEntry method, you need to first allocate memory to store the string and then copy the string to the new memory.

    Your example where you add the entries manually works because the char pointers point to static strings.

    So in your addEntry method you should do something like this:

    tempNode = (struct bookNode *)malloc(sizeof(struct bookNode));
    tempNode->firstName = (char *)malloc(strlen(fn)+1);
    strcpy(tempNode->firstName, fn);
    

    and then the same for last name and phone. Remember that you need to go through the list and free the memory for each string as well as for the nodes in the list as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I had to wrap this in an ObjectDataProvider to get… May 13, 2026 at 1:28 pm
  • Editorial Team
    Editorial Team added an answer The standard way to persist data across requests when building… May 13, 2026 at 1:28 pm
  • Editorial Team
    Editorial Team added an answer No, I don't think (A) there are common OOP idioms… May 13, 2026 at 1:28 pm

Related Questions

I am trying to create a simple dialog in MFC using Visual C++. My
I am trying to create a simple page that enters data in to a
I am trying to create a simple ajax chat using JQuery and ASP.NET. My
I am trying to create a simple mouseover effect using a combination of mouseover,
I am trying to create a simple user control that is a slider. When

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.