首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VC++ 2008中运行程序时出现未处理的异常

在VC++ 2008中运行程序时出现未处理的异常
EN

Stack Overflow用户
提问于 2012-01-17 17:29:20
回答 6查看 474关注 0票数 0

我试图打开目录中的二进制文件并执行相应的操作。我怀疑此line..is不是以下逻辑的正确表示方式。

代码语言:javascript
复制
int main(int argc, char* argv[])
{
  int k=0;
  FILE *fp;
  unsigned char cd[255];
  unsigned long cd1[500];
  char *buffer;
  unsigned long sa=80044;
  int j=0,i=0,n=0;
  DIR *dir;
  struct dirent *direntry; //could be a file, or a directory

  dir = opendir("C:/Documents and Settings/Administrator/Desktop/vicky");
  if(!dir) {
  printf("Error: directory did not open!\n");
  return 1;
  }

  while((direntry=readdir(dir))!=NULL) {
  if(++k < 100)
  {
     printf("%s\n",direntry->d_name);
     sprintf(buffer,"%s",direntry->d_name);//here i got the unhanded exception.
     fp=fopen("buffer","rb");
     sa=sa-44;
     sa=sa/8;

  if(fp==NULL)
 {
   printf("file not found!");
 }
 else
      {
       for(j=0;j<(sa);j++)
       {
           for(i=0;i<8;i++)
           {
               cd[i]=fgetc(fp);//get each character from file 

              // printf("%c",cd[i]);

           }
           if(i==8)//if the i is 8 the character then do the following,just read 8 bytes  and then calculate the cd1.
           {
                sa=sa-8;
               cd1[j]=(cd[6] * 65536 + cd[5] * 256 + cd[4]);//multiply the positional weightage and calculate the cd1,which contains the 7 digits decimal value.

               //if((cd1[j]> 0x7FFFFF)&&(cd1[j]<=0xFFFFFF))

                 //cd1[j]=cd1[j]- 0xFFFFFF;
                 //cd1[j]=cd1[i] * -1;

           }
         printf("completes first 8 bytes read:%d - %d",j,cd1[j]);//print j and cd1[j] value in console window

       }
      fclose(fp);//close the file
   }


  }
  if((strcmp(direntry->d_name, "text.txt"))==0) {
     printf("\nThe %s file has been found\n",direntry->d_name);

     k=-99;  //just a flag value to show the file was found
     break;
   }

  }
  if(k!=-99)
  printf("\nThe test.txt file was not found\n");

  closedir(dir);

  printf("\n");
  getchar();
  return 0;

 }

这是我得到的错误: READ_TEXT.exe: 0xC0000005中的0x1029a189 (msvcr90d.dll)处的未处理异常:访问冲突写入位置0xcccccccc.Kindly让我建议读取"direntry->d_ name“文件名来处理上述逻辑。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-01-17 17:34:51

为简单起见,将字符缓冲区设置为普通数组:

代码语言:javascript
复制
char buffer[1024];

这样一来,你肯定会把内存放在一边,随时可以使用。如果您的平台提供snprintf(),您应该使用它。

票数 0
EN

Stack Overflow用户

发布于 2012-01-17 17:32:58

未分配buffersprintf()基本上是在尝试写“无处可寻”。

目标变量需要使用malloc在堆上分配,或者在堆栈上分配,并将其限制在当前作用域内。

票数 0
EN

Stack Overflow用户

发布于 2012-01-17 17:33:01

您只声明了char* buffer,而没有使用malloc()为其分配内存。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8892230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档