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 8844379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:19+00:00 2026-06-14T11:28:19+00:00

I have a phonebook app that I am trying to give the functionality of

  • 0

I have a phonebook app that I am trying to give the functionality of being able to read and write files. I’ve figured out pretty easily how to write files, but reading them has really got me stuck. I think my main issue is the inability to get the file to loop(typically crashes as soon as it hits the loop). Below is my my effected functions.

Here are the structure, main, and menu functions.

typedef struct friends_contact{

   char *First_Name;
   char *Last_Name;
   char *home;
   char *cell;
}fr;
int main() {


fr friends[5];
char buffer[BUFFSIZE];
int counter=0;
int i=0;

menu(friends, &counter,i,buffer);

getch();
return 0;
}
//Menu function
void menu(fr*friends,int* counter, int i,char buffer[]) {
int user_entry=0;
int user_entry2=0;
char user_entry3[50]={'\0'};

printf("Welcome! Would you like to import a file? (1)Yes or (2) No");
scanf("%d",&user_entry);
if(user_entry==1)
     {

     file2(friends,counter,i,user_entry3);


     }else;
do{
     int result;

printf("\nPhone Book Application\n");
printf("1) Add friend\n2) Delete friend\n3) Show a friend\n4) Show  phonebook\n5)Exit\n");   
scanf("%d", &user_entry);


if(user_entry==1)
    {
        add_contact(friends,counter,i,buffer);
    }
    if(user_entry==2)
    {
        delete_contact(friends ,counter,i);
    } 
    if(user_entry==3)
    {
        result=show_contact(friends ,counter,i);
        if(result==0){
                  printf("\nName not Found\n");
                  }else{
                        result;
                        }

   }                  
   if(user_entry==4)
   {
       print_contact(friends, counter,i,user_entry3);
       file2(friends ,counter,i,user_entry3);

   } 
 }while(user_entry!=5);
   if(user_entry==5)
   {
      printf("Would you like to save entries to a file? (1)yes or (2) no");
      scanf("%d",&user_entry2);
         if(user_entry2 == 1)
         {
            printf("Please name your file");
            scanf("%s",user_entry3); 
            file(friends, counter,i,user_entry3);
            printf("Goodbye!"); 

         }else if(user_entry2 == 2){
            printf("Goodbye!"); 
       }
   }

}

Here is the function that handles the reading of the file.

void file2(fr*friends ,int* counter, int i, char user_entry3[50])
{

     FILE *read;

     printf("Please enter a file name");
     scanf("%s",user_entry3);
     read=fopen(user_entry3,"r");
    //This is where the crash is taking place!!**
     while(!feof(read)){
        fscanf(read,"%s %s %s %s",friends[i].First_Name,friends[i].Last_Name,friends[i].home,friends[i].cell);
        printf("\n""%s ""%s ""\n""<Home>""%s""\n""<Cell>""%s""\n",friends[i].First_Name,friends[i].Last_Name,friends[i].home,friends[i].cell);

      }

Now I understand there may be other problems with the program unrelated to what I’m asking, but I am new to C so this is a work in progress. I need to figure out how to stop this from crashing, and how to add this to the rest of my contacts(which I think I might have an idea for), it’s driving me crazy! thanks in advance.

  • 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-14T11:28:21+00:00Added an answer on June 14, 2026 at 11:28 am

    You are reading data into undefined memory regions (you never assign any value to the 4 string pointers in your struct). I don’t see you allocating memory for friends[i].First_Name, .Last_Name, .home and .cell.

    You might want to change your struct like this:

    typedef struct friends_contact{
       char First_Name[50+1]; // +1 for the '\0' terminating character
       char Last_Name[50+1];
       char home[50+1];
       char cell[50+1];
    }fr;
    

    Of course, if the file contains a part longer then 50 characters (including ‘\0’ termination), your code will crash again, because fscanf will not check for the length unless you specify the maximum length for each string like this:

    fscanf(read,"%50s %50s %50s %50s",friends[i].First_Name,friends[i].Last_Name,friends[i].home,friends[i].cell);
    

    If you want to use pointers in your struct, you should allocate memory to each struct member using malloc() before the reading and use free() to release the allocated memory once you don’t need it anymore.

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

Sidebar

Related Questions

I'm trying to extend my current app(internal company phonebook) to be able to forward
I am trying to build a simple phonebook in cakephp 2.0 that stores &
Ive got a phonebook app that the user can enter in contact information, and
I have a phonebook app where I generate the title for section headers by
All, I have written a PhoneBook application in Java that is command line based.
So recently I have been working on phonebook project that uses Binary Search Tree.
I have a phonebook application - an internal app in our org. I am
I have created an activity that inserts a new contact in phonebook. Now I
I'm trying to follow this tutorial on binding: http://andrehoffmann.wordpress.com/2009/09/03/phonebook-tutorial-for-dummiesxcode-3-1-3/ but the app keeps crashing
So I'm trying add malloc to a phonebook application that I created, but since

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.