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

发布于 2024-10-27 22:07:39 字数 576 浏览 5 评论 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 从数据文件中读取数据。我对其进行了调试,发现 string str 显示随机数据。当我执行此 strlen(str) 操作时,它显示 1709936 .....

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-03 22:07:39

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

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文