开发者

not able to read data from file using fread

开发者 https://www.devze.com 2023-02-20 05:32 出处:网络
unsigned long int nextOffset, currOffset, len; nextOffset = read offset from file (eg. 15) currOffset = read prev offset from file (eg. 0 )
unsigned long int nextOffset, currOffset, len;
nextOffset = read offset from file (eg. 15)
currOffset = read prev offset from file (eg. 0 )  

len = nextOffset-currOffset;  
str = malloc((int)len);
fread(str,(int)(len)-1,1,dataFile);
str[(int)len]='\0';

rowAddr = ftell(tempDataFile);
fwrite(&rowAddr,sizeof(unsigned long int),1,tempOffsetFile);
fwrite(str,(int)(len)-1,1,tempDataFile);

free(str);
开发者_如何学Go

for some reason i'm not able to read from datafile using fread.. i debugged it and what i found was that the striing str is showing random data.. when i did this strlen(str) it shows 1709936.....

what is possibly wrong with this code.. all these files are opeend in binary mode...


What Ptival said.

But the most eggregious problem is that if you allocate n bytes, they are numbered from 0 to n-1. You're setting byte n to zero, and that byte is beyond the end of what you've malloc()ed. So you're probably unintentionally stomping on some other data. C won't keep you from shooting yourself in the foot this way.

Otherwise, based on what you said you needed, there doesn't seem to be much wrong with your code. I fleshed it out a bit, and wrapped it all in a little shell script for easy running. The script:

#!/bin/sh
# pgm 1 generates a binary file.
# pgm 2 dumps a file.
# pgm 3 demos a solution to your problem.
rm -f 1 2 3;                                       \
cat > 1.c <<EOD; cat > 2.c <<EOD; cat > 3.c <<EOD; \
gcc -Wall -Werror 1.c -o 1;                        \
gcc -Wall -Werror 2.c -o 2;                        \
gcc -Wall -Werror 3.c -o 3;                        \
./1; ./2 dataFile.dat; ./3;                        \
./2 tempDataFile.dat; ./2 tempOffsetFile.dat
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char**argv)
{
  unsigned int   jndex;
  unsigned char  buffer[4];
  FILE          *phyle;

  phyle=fopen("dataFile.dat","w+");
  if(!phyle)
  {
    fprintf(stderr,"%s: fopen() fail\n",argv[0]);
    exit(1);
  }
  for(jndex='A';
      jndex<='Z';
      jndex++
     )
  {
    buffer[0]=jndex;
    if(!fwrite(buffer,sizeof(char),1,phyle))
    {
      fprintf(stderr,"%s: fwrite() fail\n",argv[0]);
    }
  }
  fclose(phyle);
  printf("%s complete\n",argv[0]);
  return 0;
}
EOD
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char**argv)
{
  int            jndex;
  unsigned char  buffer[4];
  FILE          *phyle;

  if(argc!=2)
  {
    fprintf(stderr,"%s: arg error\n",argv[0]);
    exit(1);
  }
  phyle=fopen(argv[1],"r");
  if(!phyle)
  {
    fprintf(stderr,"%s: fopen fail\n",argv[0]);
    exit(1);
  }
  for(jndex=0;
      ;
      jndex++
     )
  {
    if(!fread(buffer,sizeof(char),1,phyle))
    {
      break;
    }
    printf("%02X",buffer[0]);
    if(jndex%16==15)
    {
      printf("\n");
    }
    else
    if(jndex%2==1)
    {
      printf(" ");
    }
  }
  if(jndex%16)
  {
    printf("\n");
  }
  fclose(phyle);
  printf("%s %s complete\n",argv[0],argv[1]);
  return 0;
}
EOD
#include <stdio.h>
#include <stdlib.h>

FILE *dataPhyle;
FILE *tempDataPhyle;
FILE *tempOffsetPhyle;
void do_one_guy(char              *pgmName,
                unsigned long int  nextOffset,
                unsigned long int  curOffset
               )
{
  unsigned long int  len;
  long               rowAddr;
  char              *str;

  len=nextOffset-curOffset;
  str=malloc(len);
  if(str==NULL)
  {
    fprintf(stderr,"%s: malloc() fail\n",pgmName);
    exit(1);
  }
  if(fread(str,sizeof(char),len-1,dataPhyle)!=len-1)
  {
    fprintf(stderr,"%s: fread() fail\n",pgmName);
  }
  str[len-1]='\0';
  printf("record content is %s\n",str);
  rowAddr=ftell(tempDataPhyle);
  if(fwrite(&rowAddr,1,sizeof(rowAddr),tempOffsetPhyle)!=sizeof(rowAddr))
  {
    fprintf(stderr,"%s: fwrite(first) fail\n",pgmName);
  }
  if(fwrite(str,sizeof(char),len-1,tempDataPhyle)!=len-1)
  {
    fprintf(stderr,"%s: fwrite(second) fail\n",pgmName);
  }
  free(str);
}
int
main(int argc, char**argv)
{
  dataPhyle=fopen("dataFile.dat","r");
  if(!dataPhyle)
  {
    fprintf(stderr,"%s: fopen(\"dataFile.dat\") fail\n",argv[0]);
    exit(1);
  }
  tempOffsetPhyle=fopen("tempOffsetFile.dat","w+");
  if(!tempOffsetPhyle)
  {
    fprintf(stderr,"%s: fopen(\"tempOffsetFile.dat\") fail\n",argv[0]);
    exit(1);
  }
  tempDataPhyle=fopen("tempDataFile.dat","w+");
  if(!tempDataPhyle)
  {
    fprintf(stderr,"%s: fopen(\"tempDataFile.dat\") fail\n",argv[0]);
    exit(1);
  }
  do_one_guy(argv[0],32,16);
  do_one_guy(argv[0],12,8);
  printf("%s complete\n",argv[0]);
  return 0;
}
EOD

The output:

./1 complete
4142 4344 4546 4748 494A 4B4C 4D4E 4F50
5152 5354 5556 5758 595A 
./2 dataFile.dat complete
record content is ABCDEFGHIJKLMNO
record content is PQR
./3 complete
4142 4344 4546 4748 494A 4B4C 4D4E 4F50
5152 
./2 tempDataFile.dat complete
0000 0000 0F00 0000 
./2 tempOffsetFile.dat complete

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消