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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:45:35+00:00 2026-05-13T23:45:35+00:00

I’ve been working on this assignment, where I need to read in records and

  • 0

I’ve been working on this assignment, where I need to read in “records” and write them to a file, and then have the ability to read/find them later. On each run of the program, the user can decide to write a new record, or read an old record (either by Name or #)

The file is binary, here is its definition:

typedef struct{
        char * name;
        char * address;
        short addressLength, nameLength;
        int phoneNumber;
    }employeeRecord;
    employeeRecord record;

The way the program works, it will store the structure, then the name, then the address. Name and address are dynamically allocated, which is why it is necessary to read the structure first to find the size of the name and address, allocate memory for them, then read them into that memory.

For debugging purposes I have two programs at the moment. I have my file writing program, and file reading.

My actual problem is this, when I read a file I have written, i read in the structure, print out the phone # to make sure it works (which works fine), and then fread the name (now being able to use record.nameLength which reports the proper value too).
Fread however, does not return a usable name, it returns blank.

I see two problems, either I haven’t written the name to the file correctly, or I haven’t read it in correctly.
Here is how i write to the file: where fp is the file pointer. record.name is a proper value, so is record.nameLength. Also i am writing the name including the null terminator. (e.g. ‘Jack\0’)

fwrite(&record,sizeof record,1,fp);
fwrite(record.name,sizeof(char),record.nameLength,fp);
fwrite(record.address,sizeof(char),record.addressLength,fp);

And i then close the file.
here is how i read the file:

fp = fopen("employeeRecord","r");


fread(&record,sizeof record,1,fp);
printf("Number: %d\n",record.phoneNumber);


char *nameString = malloc(sizeof(char)*record.nameLength);

printf("\nName Length: %d",record.nameLength);
fread(nameString,sizeof(char),record.nameLength,fp);
printf("\nName: %s",nameString);

Notice there is some debug stuff in there (name length and number, both of which are correct). So i know the file opened properly, and I can use the name length fine. Why then is my output blank, or a newline, or something like that? (The output is just Name: with nothing after it, and program finishes just fine)

Thanks for the help.

  • 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-13T23:45:36+00:00Added an answer on May 13, 2026 at 11:45 pm

    I tried your code and it worked fine. In order, here is the output, a hexdump of the file, and your source made to compile.

    Update: Updated code to read name and address from stdin or command-line arguments.

    prompt$ g++ -g -Wall -o test_records test_records.cpp
    prompt$ echo -e "Test User\nSomeplace, Somewhere" | ./test_records
    sizeof(employeeRecord) = 24
    Number: 5551212
    
    Name Length: 9
    Name: Test User
    
    prompt$ hexdump -C employeeRecord 
    00000000  90 f7 bf 5f ff 7f 00 00  70 f7 bf 5f ff 7f 00 00  |..._....p.._....|
    00000010  14 00 09 00 6c b4 54 00  54 65 73 74 20 55 73 65  |....l.T.Test Use|
    00000020  72 53 6f 6d 65 70 6c 61  63 65 2c 20 53 6f 6d 65  |rSomeplace, Some|
    00000030  77 68 65 72 65                                    |where|
    00000035
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct{
            char * name;
            char * address;
            short addressLength, nameLength;
            int phoneNumber;
        }employeeRecord;
    
    int main(int argc, char *argv[])
    {
      employeeRecord record;
    
    #if 0
      // Commmand line arguments
      if (argc < 3)
        return 1;
    
      record.nameLength = strlen(argv[1]);
      record.name = (char *)malloc(sizeof(char)*(record.nameLength + 1));
      strncpy(record.name, argv[1], record.nameLength + 1);
    
      record.addressLength = strlen(argv[2]);
      record.address = (char *)malloc(sizeof(char)*(record.addressLength + 1));
      strncpy(record.address, argv[2], record.addressLength + 1);
    #else
      // stdin
      char input[1024];
    
      fgets(input, sizeof(input), stdin);
      record.nameLength = strlen(input);
      record.name = (char *)malloc(sizeof(char)*(record.nameLength + 1));
      strncpy(record.name, input, record.nameLength + 1);
    
      fgets(input, sizeof(input), stdin);
      record.addressLength = strlen(input);
      record.address = (char *)malloc(sizeof(char)*(record.addressLength + 1));
      strncpy(record.address, input, record.addressLength + 1);
    #endif
    
      record.phoneNumber = 5551212;
    
      FILE *fp = NULL;
    
      printf("sizeof(employeeRecord) = %lu\n", sizeof(employeeRecord));
    
      // Write
      fp = fopen("employeeRecord","w");
      fwrite(&record,sizeof(employeeRecord),1,fp);
      // Note: we're not including terminating NULLs.
      fwrite(record.name,sizeof(char),record.nameLength,fp);
      fwrite(record.address,sizeof(char),record.addressLength,fp);
      fclose(fp);
    
      // Read
      fp = fopen("employeeRecord","r");
      fread(&record,sizeof(employeeRecord),1,fp);
      printf("Number: %d\n",record.phoneNumber);
    
      char *nameString = (char *)malloc(sizeof(char)*(record.nameLength + 1));
      printf("\nName Length: %d",record.nameLength);
      fread(nameString,sizeof(char),record.nameLength,fp);
      nameString[record.nameLength] = '\0';
      printf("\nName: %s",nameString);
      printf("\n");
    
      fclose(fp);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.