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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:03:57+00:00 2026-06-13T10:03:57+00:00

I am taking some information from a user (name, address, contact number) and store

  • 0

I am taking some information from a user (name, address, contact number) and store it in a struct. I then store this in a file which is opened in “r+” mode. I try reading it line by line and see if the entry I am trying to enter already exists, in which case I exit. Otherwise I append this entry at the end of the file. The problem is that when I open the file in “r+” mode, it gives me Segmentation fault!

Here is the code:

struct cust{
    char *frstnam;
    char *lastnam;
    char *cntact;
    char *add;
};

Now consider this function. I am passing a struct of information in this function. Its job is to check if this struct already exists else append it to end of file.

void check(struct cust c)
{
    struct cust cpy;
    FILE *f;    
    f=fopen("Customer.txt","r+"); 
    int num=0;

    if (f!= NULL){
        while (!feof(f)) {
            num++;
            fread(&cpy,sizeof(struct cust),1,f);

            if ((cpy.frstnam==c.frstnam)&(cpy.lastnam==c.lastnam)&(cpy.cntact==c.cntact)&(cpy.add==c.add))
            {
                printf("Hi %s %s. Nice to meet you again. You live at %s and your contact number is %s\n", cpy.frstnam,cpy.lastnam,cpy.add,cpy.cntact);
                return;
            }
        }
        fwrite(&c,sizeof(struct cust),1,f);
        fclose (f);
    }
    printf("number of lines read is %d\n",num);
}
  • 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-13T10:03:58+00:00Added an answer on June 13, 2026 at 10:03 am

    The problem is that your structure contains pointers to strings and not strings themselves. So freading and fwriting will not work because the pointer values will be read and written but aren’t valid between runs of the application.

    A simple fix would be to change the structure to:

    struct cust{
    char frstnam[25];
    char lastnam[25];
    char cntact[25];
    char add[25];
    };
    

    It’s not a great fix, but it is a fix and might work for you.

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

Sidebar

Related Questions

I am making an application in which I am taking some information from user
I'm taking some information from data base and i want to use it in
I have this query that is taking information from two different databases: I'm using
This problem is taking me too long to fix I could use some guidance
I would like to extract 2 pieces information from a text file using the
I've been give this layout where I have a section, with some information. However,
This is a two part question, but first some background information: I have a
I have a very simple SQL query that I made which retrieves some user
Thanks for taking some time to help me out. Using: Microsoft Visual C# 2010
I've got a struts2 action that responds to an AJAX request by taking some

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.