Using fread to store sequence of bytes from file to dynamic array

Posted on 2004-11-08
Last Modified: 2012-06-27
What I'm trying to do is read bytes from a file (infile) and store them into a dynamic array (so later on I can output each byte in hex, octal, binary). I would appreciate any suggestions regarding my code below and if this approach will help me with the task at hand.
My code for the store funtion:

/* s and e represent the first and last bytes of the file to be stored. byte_n is the total
    number of bytes in the infile */

char * store_in_array(int s, int e, int byte_n) {
  char *byteptr;
  size_t size;
  size = ((size_t)e - (size_t)s);          
       /* allocate an array of length size, where */
            /* each element is 1 byte */

     f (( byteptr = (char*)malloc(1 * size)) == NULL)  
           printf("Storage allocation failed.\n");
  /* set file offset to first byte to be read */
     fseek(infile, start, SEEK_SET);    
   /* read size bytes to array byteptr */
  if ( fread((char*)&byteptr, 1, size, infile) == 0)
   printf("Problem in fread().\n");
  return byteptr;


Question by:Kevin_P23
    LVL 86

    Accepted Solution

    The only problem I see is

      if ( fread((char*)&byteptr, 1, size, infile) == 0)

    That should be

      if ( fread(byteptr, 1, size, infile) == 0)

    because otherwise you are passing the address of 'byteptr' itself and not the memory are it points to.

    Author Comment

    I gotcha...
    The function works properly now, and I see the reasoning behind what I was doing wrong (the library function prototypes can be confusing sometimes).  

    Thanks again.


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now