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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:16:19+00:00 2026-06-12T06:16:19+00:00

Having an issue with my fwrite() and fread() dealing with a binary file here

  • 0

Having an issue with my fwrite() and fread() dealing with a binary file here is my source code, and towards the bottem are my read and write. Right now it is returning ‘jake’ when I run it, and nothing else. I was told to write a dump buffer function to deal with the binary characters. In addition here is the text file as well, I am writing to a blank file called info.bin. PS I know that it is bad practice to save the zip as an int but this is what my professor is asking for.

File:

mike|203-376-5555|7 Melba Ave|Milford|CT|06461
jake|203-555-5555|8 Melba Ave|Hartford|CT|65484
snake|203-555-5555|9 Melba Ave|Stamford|CT|06465
liquid|203-777-5555|2 Melba Ave|Barftown|CT|32154

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LINE 80
#define RECORDS 10


struct info{
  char name[100];
  char number[100];
  char address[100];
  char city[100];
  char state[100];
  int zip;
};

void dump_buffer(void *buffer, int buffer_size)
{
  int x;

  for(x = 0; x < buffer_size; x++)
  {
    printf("%c",((char *)buffer)[x]);
  }
}

int i, j, seeker;

int main(int argc, char* argv[])
{
  char *buffer;

  struct info input_records[RECORDS];
  int nrecs = 0;
  unsigned long fileLen;
  char line[LINE];
  FILE *fp = NULL;
  FILE *fpbin = NULL;
  FILE *fpread = NULL;



  if (argc != 2) 
  {
    printf ("ERROR: you must specify file name!\n");
    return 1;
  }
  /* Open file */
  fp = fopen(argv[1], "r");
  if (!fp) 
  {
    perror ("File open error!\n");
    return 1;
  }

  while (!feof (fp)) {
    fgets(line, sizeof(line),fp);
    char* tok =  strtok(line, "|");

    while(tok != NULL)
    {
      strcpy(input_records[nrecs].name, tok);
      tok = strtok(NULL, "|");
      strcpy(input_records[nrecs].number, tok);
      tok = strtok(NULL, "|");
      strcpy(input_records[nrecs].address, tok);
      tok = strtok(NULL, "|");
      strcpy(input_records[nrecs].city, tok);
      tok = strtok(NULL, "|");
      strcpy(input_records[nrecs].state, tok);
      tok = strtok(NULL, "|");
      input_records[nrecs].zip = atoi(tok);
      tok = strtok(NULL, "|");
    }
    nrecs++;
  }




  fpbin = fopen("info2.bin", "wb");
  if (!fp) 
  {
    perror ("File open error!\n");
    return 1;
  }

  for(i = 0; i < 4; i++)
  {

   fwrite(&input_records[i], sizeof(struct info), 200000, fpbin);

  }

  fclose(fpbin);



  fpread = fopen("info2.bin", "rb");



  fseek(fpread, 0, SEEK_END);
  fileLen = ftell(fpread);
  fseek(fpread, 0, SEEK_SET);

  buffer = (char *)malloc(sizeof(struct info));

  fread(buffer, fileLen, 1, fpread);


  dump_buffer(buffer, sizeof(buffer));

  fclose(fpread);

  fclose(fp);
  free(buffer);
return 0;
}
  • 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-12T06:16:20+00:00Added an answer on June 12, 2026 at 6:16 am
    fwrite(&input_records[i], sizeof(struct info), 200000, fpbin);
    

    You just told fwrite to write 200000 * sizeof(struct info) bytes to the file, starting at the address of input_records[i]. That accesses memory far beyond what has been allocated for input_records, the behaviour is undefined, but a segmentation fault is not unlikely. I’m actually surprised that apparently it didn’t crash for you.

    buffer = (char *)malloc(sizeof(struct info));
    
    fread(buffer, fileLen, 1, fpread);
    

    You’re trying to read fileLen bytes into a buffer of size sizeof(struct info). If fileLen > sizeof(struct info), that is again undefined behaviour, and if fileLen is sufficiently larger, likely to crash.

    You should let fwrite one object of size sizeof(struct info) each time, and you should allocate fileLen bytes for the buffer you read in (or read in chunks of size sizeof(struct info)). And you should check the return values of fwrite and fread to know whether they succeeded in writing/reading the desired data and handle failures appropriately.

    fpbin = fopen("info2.bin", "wb");
    if (!fp) 
    {
    

    You check the wrong FILE* here, and you don’t check fpread at all.

    Further, you pass the wrong count to dump_buffer,

    dump_buffer(buffer, sizeof(buffer));
    

    buffer is a char*, so sizeof buffer is the size of a char*, typically four or eight bytes. You should pass the allocated size of the buffer there.

    And, when reading the original file,

    while (!feof (fp)) {
        fgets(line, sizeof(line),fp);
    

    feof(fp) only becomes true after an attempt to read was made when the end of the file has been reached, you should change your loop condition to

    while(fgets(line, sizeof line, fp) != NULL) {
    

    Finally, your tokenizing code will fail badly if the input file contains malformed data, or too long lines. You should also add checks there, so that you don’t pass a NULL to strcpy or atoi.

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

Sidebar

Related Questions

Having an issue here that I have tried everything I can think of but
having issue with the explode, i have simple text file and each line is
Having issue with my navigation. How can I prevent it? My CSS file looks
Im having issue with mail sending In web.config file <mailSettings> <smtp from=xxxx@companyname.com> <network host=mail.authsmtp.com
I'm having issues getting a php form to write to a file hosted with
I am trying to implement BST algorithm using Cormen's pseudo code yet having issue.
I am having issue in compiling code having templated classes nested within other templated
I'm having issue with a simple code downloading a remote image and adding content
I had WPF ListView in my xaml file and having issue to multiselect rows
I am having issue making a form be sent using ajax. Here is my

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.