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

由于某种原因,我无法使用 fread 从数据文件中读取数据。我对其进行了调试,发现字符串 str 正在显示随机数据。当我执行此 strlen(str) 时,它显示 1709936 .....

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

4

1 回答 1

0

Ptival所说的。

但最令人讨厌的问题是,如果分配n字节,它们的编号是从 0 到n-1。您将 byte 设置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

希望这可以帮助。

于 2011-03-27T08:48:50.597 回答