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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:46:43+00:00 2026-06-14T02:46:43+00:00

I need to do an assignment for a class I’m taking. It’s a simple

  • 0

I need to do an assignment for a class I’m taking.
It’s a simple phonebook app in C, and i’m having a little trouble with it since I need to use some new stuff in the program and the deadline is pretty tight.

I looked around and found some answers, but a new one came up every time. 🙂

This is my (simplified) program:

typedef struct record
{
    char fname[31];
    char lname[31];
    char tel[21];
    struct record *next;
} record;


record *new_entry(record *first, char *fname, char *lname, char *tel)
{
    record *new;
    new=(record*) malloc(sizeof(record));
    strcpy(new->fname, fname);
    strcpy(new->lname, lname);
    strcpy(new->tel, tel);
    new->next=first;
}


void fileopen (char *db_file)
{
    FILE *fp;  

    fp=fopen(db_file, "rb");
    if (fp==NULL) 
    {
        fp=fopen(db_file, "wb");
        fclose(fp);
        fp=fopen(db_file, "r+b");
    }
}



int main
{
 char db[51];
 record *next = NULL;

 printf("File:           "); scanf("%s, db);
 fileopen(db);
 printf("First name:     "); scanf("%s", fname);
 printf("Last name:      "); scanf("%s", lname);
 printf("Phone number:   "); scanf("%s", tel);
 first=new_entry(*first, fname, lname, tel);
}

I left out the unessential parts.
Now I know it’s not much, but my class leader said I should use binary files to store and restore the data. But I got really confused if how am I supposed to use fread and fwrite.


Thanks so much for the solution! I think I start to understand the concept.
The program now stores the data (At least I think it does, because the file keeps growing as I add more data.)
When starting a fresh binary file, the program correctly displays the data when requested, but if I close it, and reopen the same file, nothing happens when it tries to list the contacts.

Here’s the (simplified again, i have 10 details in one record in the actual assignment) open function:

record *open (char *db_file, record start)
{
  FILE *fp
  record *temp = start;
  fp=fopen(db_file, "rb");
  while (fread(temp, sizeof(rekord), 1, fp)>0) 
    {
        fread(temp->fname, sizeof temp->fname, 1, fp);
        fread(temp->lname, sizeof temp->lname, 1, fp);
        fread(temp->tel, sizeof temp->tel, 1, fp);
    temp=temp->next;
    }
  fclose(fp);
  return temp;
}

And in main(), I use:

start=open(db, start);

The declaration part:

record *start=NULL;

Thanks again if someone replies.

  • 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-14T02:46:45+00:00Added an answer on June 14, 2026 at 2:46 am

    To write your linked list to a file, you can run through the list, and write your structure.

    #include <stdio.h>
    
    record *it = first;
    
    while (it != NULL) {
        fwrite (it->fname, sizeof it->name, 1, stream);
        fwrite (it->lname, sizeof it->lname, 1, stream);
        fwrite (it->tel, sizeof it->tel, 1, stream);
        it = it->next;
    }
    

    stream is a file you can open with wb mode in fopen.

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

Sidebar

Related Questions

I am writing a Date class for an assignment and I am having trouble
I need to run some basic SQL code for a class assignment, but I'm
For a class assignment I need to implement a destructor for the objects of
I am doing a homework assignment for my summer OO class and we need
I am doing a homework assignment for my summer OO class and we need
I need a class room definition for IP-aware for a research assignment. I.E. something
In the assignment operator of a class, you usually need to check if the
For an assignment I need to create a class within class Point from java.awt.Point.
I'm working on an assignment for a class, and need a bit of direction.
I have an assignment and I need to make my own (simple) generic linked

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.