无法使用fread从文件中读取数据

时间:2011-03-27 04:49:57

标签: c malloc long-integer unsigned fread

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);

由于某种原因,我无法使用fread读取数据文件..我调试它,我发现striing str显示随机数据..当我做这个strlen(str)它显示1709936 .. ...

这段代码可能有什么问题..所有这些文件都以二进制模式运行...

1 个答案:

答案 0 :(得分:0)

Ptival 说什么。

但最令人不快的问题是,如果你分配n字节,它们的编号从0到n - 1。您将字节n设置为零,并且该字节超出了您malloc()编辑的结尾。所以你可能无意中踩到了其他一些数据。 C不会阻止你以这种方式射击自己。

否则,根据您所说的内容,您的代码似乎没有太大问题。我把它充实了一点,并将它全部包装在一个小的shell脚本中,以便于运行。脚本:

#!/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

输出:

./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

希望这有帮助。