Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-25-2006, 11:03 PM   #1
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Reading and Writing integers to binary file


I have an array block of 16 hexadecimal integer values. I'm trying to write them to a file, and then open the file and read them back. I've been trying the write them with the following call:

fwrite(&block, sizeof(integer), 16, fout);
I'm then trying to read these back with the following:
fread(&block, sizeof(integer), 16, fin);
Unfortunately, I'm not getting back what was originally in the block array. Anyone have any suggestions?

Thanks for any help.

By the way, this is how I'm opening the files:
fout = fopen("out.dat", "wb");
fin = fopen("out.dat", "rb");
Old 02-26-2006, 12:01 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
What you've got looks fine. As far as I can tell.

Here's a standalone program based on your code, and the resulting output:
#include <stdio.h>
#include <string.h>

#define NELMS 16

main(int argc, char *argv[])
  // Declare a struct and and array
  struct mystruct
    int a[NELMS];
  } mystruct;
  int myarray[NELMS];

  // Store the same data in both
  int i;
  printf ("Clearing %d bytes in mystruct, %d bytes in myarray...\n",
    sizeof (mystruct), sizeof (myarray));
  memset (&mystruct, 0xaa, sizeof (mystruct));
  memset (myarray, 0xaa, sizeof (myarray));
  for (i = 0; i < NELMS; i++)
    mystruct.a[i] = myarray[i] = i;

  // Save the data
  FILE *fp = fopen ("out.dat", "wb");
  if (fp == NULL)
    perror ("Unable to open out.dat for writing!\n");
    return 1;
  fwrite (&mystruct, sizeof (int), NELMS, fp);
  fwrite (myarray, sizeof (int), NELMS, fp);
  fclose (fp);

  // Clear the buffers again
  memset (&mystruct, 0xaa, sizeof (mystruct));
  memset (myarray, 0xaa, sizeof (myarray));

  // Try reading it back
  fp = fopen ("out.dat", "rb");
  if (fp == NULL)
    perror ("Unable to open out.dat for reading!\n");
    return 1;
  fread (&mystruct, sizeof (int), NELMS, fp);
  fread (myarray, sizeof (int), NELMS, fp);
  fclose (fp);

  // Display the results
  for (i = 0; i < NELMS; i++)
    printf ("mystruct.a[%2d] = 0x%02x, myarray[%2d] = 0x%02x...\n",
      i, mystruct.a[i], i, myarray[i]);
  return 0;
cc -g -o xxx xxx.c
Clearing 64 bytes in mystruct, 64 bytes in myarray...
mystruct.a[ 0] = 0x00, myarray[ 0] = 0x00...
mystruct.a[ 1] = 0x01, myarray[ 1] = 0x01...
mystruct.a[ 2] = 0x02, myarray[ 2] = 0x02...
mystruct.a[ 3] = 0x03, myarray[ 3] = 0x03...
mystruct.a[ 4] = 0x04, myarray[ 4] = 0x04...
mystruct.a[ 5] = 0x05, myarray[ 5] = 0x05...
mystruct.a[ 6] = 0x06, myarray[ 6] = 0x06...
mystruct.a[ 7] = 0x07, myarray[ 7] = 0x07...
mystruct.a[ 8] = 0x08, myarray[ 8] = 0x08...
mystruct.a[ 9] = 0x09, myarray[ 9] = 0x09...
mystruct.a[10] = 0x0a, myarray[10] = 0x0a...
mystruct.a[11] = 0x0b, myarray[11] = 0x0b...
mystruct.a[12] = 0x0c, myarray[12] = 0x0c...
mystruct.a[13] = 0x0d, myarray[13] = 0x0d...
mystruct.a[14] = 0x0e, myarray[14] = 0x0e...
mystruct.a[15] = 0x0f, myarray[15] = 0x0f...
Old 02-26-2006, 12:27 AM   #3
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30

Thanks for your help. That works very nicely. I'm not sure what I was doing before.

Thanks again for your help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary reading/writing in cpp kornerr Programming 1 11-27-2005 07:09 PM
Reading negative integers from a file? (C++, iostream library) -=zAe=- Programming 5 02-21-2005 12:59 PM
problem in reading Microsoft word as a binary file ljqu_happy Programming 15 02-02-2005 10:10 AM
Problem in reading/writing binary data in Linux esi-eric Linux - Hardware 3 07-20-2004 04:21 PM
Reading integers in a GTK textbox... Claus Programming 2 11-04-2003 08:44 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration