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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:31:29+00:00 2026-05-27T10:31:29+00:00

First to tell you my issue: I have written a program that based on

  • 0

First to tell you my issue:

I have written a program that based on this struct..

typedef struct{ 
    char firstname [40];    
    char lastname [40]; 
    char address [100]; 
    char phone[11];
}contact;

will ask the user for input and then write it to a file. Except that when I do write to the file, it writes i.e for first name, the rest of the 40 characters and so everything is spaced out..

A colleague of mine suggest creating a structure of pointers but I’m unfamiliar with this concept completely. I have this so far in replacement of the above structure:

typedef struct{ 
    char* firstname;    
    char lastname [40]; 
    char address [100]; 
    char phone[11];
}contact;

in which case I’m only working on first name right now. I have seen examples that create a separate array

char comm[100];

fgets(entry.firstname, 40, stdin);
entry.firstname = new char[strlen(comm)];

or something to this affect but this is giving me fits. What I want is to have the person enter an entry in and the size of the field grow and shrink to what is entered by the user. Any fixes would be appreciated!

Sorry for leaving this out :/ :

pFile = fopen("C:\\contacts.txt", "r+");

if(!pFile){
    puts("File could not be open.");
    return 1;
    }

fwrite(&entry,1,sizeof(entry),pFile); 
  • 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-27T10:31:30+00:00Added an answer on May 27, 2026 at 10:31 am

    The problem is that you’re just dumping the entire contact structure to a file. What you want to do instead is write out only the characters the user entered. Fortunately, this is easy. To get started, let’s just print out the first name:

    fprintf(pFile, "%s\n", entry.firstname);
    

    If you’re using C++, you should use std::string instead of character arrays. Your struct can now look like

    typedef struct{ 
        std::string firstname;    
        std::string lastname; 
        std::string address; 
        std::string phone;
    }contact;
    

    And you can write to a file using fstream

    std::ofstream outfile("C:\\contacts.txt");
    outfile << contact.firstname << std::endl
            << contact.lastname << std::endl
            << contact.address << std::endl
            << contact.phone << std::endl;
    outfile.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a java program that does sort of this: It starts off with
I know that I need to tell my UITextField to resign first responder when
First off, there's a bit of background to this issue available on my blog:
ok, so i can't tell if this is a server issue or what. i've
I am creating buttons dynamically for a user-based app. Now I have to tell
I have a JEE6 project based on Glassfish 3.1.1 that is moving beyond the
I have this C function which attempts to tell me if a sub string
I have a RoR site that loads a splash page on the first visit.
I would like to have an advice for this issue: I am using Jbos
I have written an extension in Google Chrome that opens a tab to a

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.