?
Solved

fread problem

Posted on 2009-04-30
2
Medium Priority
?
251 Views
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 ...

Thanx
#include <stdio.h>
#include <stdlib.h>
 
#define ID_LENGTH 9
#define NAME_LENGTH 12
#define SURNAME_LENGTH 15
#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
#define ERR_MEMORY_ALLOCATE -2
 
 
typedef struct {
  int salary;
  char id[ID_LENGTH];
  char name[NAME_LENGTH];
  char surname[SURNAME_LENGTH];
  char origin[ORIGIN_LENGTH];
}person;
 
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");
  else
    return 0;
 
  Test = (person *)malloc(sizeof(person));
 
  for (i=0;i<500;i++)
    GetThisRecord(fp,i,Test,&IsActive);  
	
  fclose(fp);    
  system("pause");	
  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);
    system("pause");
    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",
    RecordData->salary,
    RecordData->id,
    RecordData->name,
    RecordData->surname,
    RecordData->origin,*isActive,*isActive);
 
  return SUCCESS;
}

Open in new window

0
Comment
Question by:ikaros23
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 750 total points
ID: 24268386
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.
0
 

Author Closing Comment

by:ikaros23
ID: 31576353
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 !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

864 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