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

  • Home
  • SEARCH
  • 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 8870109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:38:35+00:00 2026-06-14T17:38:35+00:00

When I read information from a file into my phonebook program and try to

  • 0

When I read information from a file into my phonebook program and try to view it via the contacts list, the list is blank the first time you try and view it, but if you choose to check it again, the content from the file is there. Even weirder, when you add a name via the program, the name shows up once along with the file data, but then if you check it again, the new name is gone. If you delete a name, it deletes it once, but if you check the contacts list again, the name comes back. I’m at a loss. Here is my Read Function and my Print function.

void file2(fr*friends ,int* counter, int i,char buffer[],FILE*read,char user_entry3[])
{
    int len;
    fr temp;
    *counter=0;
    i=0; 
    fseek(read, 0, SEEK_SET);  
    while (fgets(buffer, 80, read) != NULL) { 
        temp.First_Name=malloc(36); //was j+1
        temp.Last_Name=malloc(36); //strlen(buffer));

        sscanf(buffer,"%s %s",temp.First_Name,temp.Last_Name);

        fgets(buffer, 20, read);
        len=strlen(buffer);
        if(buffer[len-1]=='\n')
            buffer[len-1]='\0';

        temp.home=malloc(20); //len);
        strcpy(temp.home, buffer);

        fgets(buffer, 20, read);
        len=strlen(buffer);
        if(buffer[len-1]=='\n')
            buffer[len-1]='\0';


        temp.cell=malloc(20); //len);
        strcpy(temp.cell, buffer); 

        friends[i].First_Name=malloc(MAXNAME);
        friends[i].Last_Name=malloc(MAXNAME);
        friends[i].home=malloc(MAXPHONE);
        friends[i].cell=malloc(MAXPHONE);

        if(!friends[i].First_Name || !friends[i].Last_Name || !friends[i].home || !friends[i].cell) {
            printf("\nmalloc() failed!\n");
            getchar();
            return;
        }

        strcpy(friends[*counter].First_Name,temp.First_Name);
        strcpy(friends[*counter].Last_Name,temp.Last_Name);
        strcpy(friends[*counter].home,temp.home);
        strcpy(friends[*counter].cell,temp.cell);


        (*counter)++;
        i++; 

    }
    //fclose(read);
    free(temp.Last_Name);
    free(temp.First_Name);
    free(temp.home);
    free(temp.cell);
}


void print_contact(fr*friends ,int* counter, int i,char buffer[],char    user_entry3[50],FILE*read) {

    for( i = 0; i < *counter; i++)

        if (strlen(friends[i].First_Name) && strlen(friends[i].Last_Name)&&     strlen(friends[i].home)&& strlen(friends[i].cell ))
        {

            getFirst(friends, i);
            getLast(friends, i);
            getHome(friends, i);
            getCell(friends, i);



        }
    file2(friends ,counter,i,buffer,read,user_entry3);

}

It’s weird because it’s like the program works perfectly once, but then not again.

  • 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-06-14T17:38:36+00:00Added an answer on June 14, 2026 at 5:38 pm

    It’s weird because it’s like the program works perfectly once, but then not again.

    I’m afraid it really is only as if it worked:

    • friends is preallocated somewhere? For you just keep reading input from file and incrementing i till the end of file (or error) – that’s likely to overflow sooner or later;

    • you read 80 characters, and then try to put it to 2 times 36 characters – that can easily overflow again. Not mentioning reading those 80 into a buffer that is passed as argument through several functions – why not to use local variable? Is the magic number 36 smaller, larger or equal to MAXNAME? Why?

    • malloc() temp on every iteration of the loop but free() it only once after it finishes – leaking memory ~120B/entry;

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

Sidebar

Related Questions

Assignment: Write a C++ program to read the menu information from menu.txt. First letter
My windows program receives information from another program via directory/file interface. That is the
I have a function that is supposed to read from a file into a
I want to read in a list of information from a specified url that
I'm making an app which tries to read the information from the .plist file
I would like to read from a file into an unsigned character buffer as
I'm trying to import tariff information from an Excel file into a DataTable to
I want to read graph adjacency information from a text file and store it
I want to read my data from my .txt file into a ReaderClass in
i have developed application for read information from card reader. Here i have used

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.