fread problem

Posted on 2009-04-30
Last Modified: 2012-05-06
I am using the code below (as a part of a larger program) to read some records from a binary file. In the GetThisRecord function the fread reads records from the file I have attached but in some records it fails to read a RECSIZE size into the Buffer. from the messages I used (I left them in the code ) I have come to the conclusion that for 5 records it reads 512 bytes instead of 56 that is the RECSIZE (I discovered this by using ftell before fread and after fread and in all records the  filepointer  was moved by 512 bytes. I cannot find ant problem in the file ...

#include <stdio.h>

#include <stdlib.h>

#define ID_LENGTH 9

#define NAME_LENGTH 12


#define ORIGIN_LENGTH 15

#define ACTIVE 195

#define INACTIVE 196

#define YES 'Y'

#define NO 'N'

#define RECSIZE (sizeof(int) + ID_LENGTH + NAME_LENGTH + SURNAME_LENGTH + ORIGIN_LENGTH + sizeof(char))

#define SUCCESS 1

#define ERR_EOF -1


typedef struct {

  int salary;

  char id[ID_LENGTH];

  char name[NAME_LENGTH];

  char surname[SURNAME_LENGTH];

  char origin[ORIGIN_LENGTH];


FILE *SO_init(char *filename);

int GetThisRecord(FILE *fp,int CurrRecord,person *RecordData,char *isActive);

int main(int argc, char *argv[])


  FILE *fp;

  char IsActive;

  person *Test;

  int i;

  if ((fp = SO_init("credit.bin")) !=NULL)

    printf("File Opened\n");


    return 0;

  Test = (person *)malloc(sizeof(person));

  for (i=0;i<500;i++)





  return 0;


FILE *SO_init(char *filename)


  FILE *fp;

  if ((fp = fopen(filename,"r"))==NULL)

    fp = fopen(filename,"wb");


  if (fp != NULL)


    //Perform actions during open.



  return fp;       



int GetThisRecord(FILE *fp,int RecNo,person *RecordData,char *isActive)


  char Buffer[RECSIZE], *ptrBuffer;

  int OriginalPos;  


  OriginalPos = ftell(fp);  

  if (fseek(fp, RECSIZE * RecNo ,SEEK_SET) != 0 ) 


    printf("Could Not Recover Record %d \n",RecNo);

    return ERR_EOF;


  printf("%d (%d) ",ftell(fp), RECSIZE * RecNo);

  memset((void *)Buffer,0,RECSIZE);


  if ( fread((void *)Buffer,RECSIZE,1,fp) != 1)


    printf("2.Could Not Recover Record %d --> %d .. (%d)\n",RecNo, ftell(fp), RECSIZE);


    return ERR_EOF;



  *isActive = Buffer[RECSIZE-1];

  ptrBuffer = Buffer;

  memcpy((void *)(&(RecordData->salary)),(void *)ptrBuffer,sizeof(int));  

  ptrBuffer += 4;


  memcpy((void *)RecordData->id,(void *)ptrBuffer,ID_LENGTH);

  ptrBuffer += ID_LENGTH;


  memcpy((void *)RecordData->name,(void *)ptrBuffer,NAME_LENGTH);

  ptrBuffer += NAME_LENGTH;


  memcpy((void *)RecordData->surname,(void *)ptrBuffer,SURNAME_LENGTH);

  ptrBuffer += SURNAME_LENGTH;


  memcpy((void *)RecordData->origin,(void *)ptrBuffer,ORIGIN_LENGTH);

  ptrBuffer += ORIGIN_LENGTH;  


//  if ( *isActive == 195)

  printf(" %d %s %s %s %s --> %c %i\n",






  return SUCCESS;


Open in new window

Question by:ikaros23
    LVL 53

    Accepted Solution

    First of all, you should open the file in binary mode. Especially if you want to perform fseek's etc.

    See if that "fixes" your problem.

    Author Closing Comment

    Thanx man ... I feel like an idiot sometimes..... It worked fine ... I had my attention on the GetThisRecord funtion and not even noticed that I had opened the file in text mode .... THANX !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now