2012-01-17 125 views
0

我試圖在目錄中打開一個二進制文件,並相應地執行相應的操作。我懷疑這條線..不是正確的表示方式來處理下面的邏輯。在VC++ 2008中運行程序時出現未處理的異常

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未處理的異常在0x1029a189(msvcr90d.dll):0000005:訪問衝突寫入位置0xcccccccc.Kindly讓我任何建議閱讀「direntry-> d_name」文件名稱來處理上述邏輯。

+0

緩衝區尚未分配爲指向任何內容 – CashCow 2012-01-17 09:34:52

+0

修復分配問題後,嘗試打開名爲「buffer」的文件。將調用中的字符串更改爲'fopen'作爲變量'buffer'而不是字符串'「buffer」'。另外,爲了獲得成功,您可能需要在'buffer'變量中具有文件的完整路徑。 – 2012-01-17 09:43:23

+0

感謝joachim,我可以讀取目錄中的每個文件並在控制檯窗口中顯示。之後,我試圖打開文件..whcih不在這裏發生。我改變了緩衝區作爲variable.not string.dir varaible包含目錄路徑?。是否有任何其他方式正確工作? – user1133907 2012-01-17 10:10:08

回答

0

使字符緩衝區普通陣列,爲簡單起見:

char buffer[1024]; 

這樣,你一定有內存放放在一旁,隨時可以使用。如果您的平臺提供它,您應該使用snprintf()

+0

感謝您的回覆......它的工作..但使用sprintf()本身.MS VC++ 2008是我正在使用的編譯器。 – user1133907 2012-01-17 09:52:32

+0

這裏沒有找到文件!控制檯輸出的文件名。例如:找不到文件! filename.txt.but不是實際的輸出如預期的那樣,爲什麼我無法打開該目錄中的文件,並執行其他操作...請任何想法。 – user1133907 2012-01-17 09:59:23

0

buffer未分配。 sprintf()基本上是試圖寫「無處」。

目標變量需要在堆上使用malloc進行分配,或者在堆棧上將其用於當前範圍。

0

您只聲明瞭char *緩衝區,不使用malloc()爲其分配內存。

0

你的問題是未初始化的指針。

初始化使用malloc的指針或嘗試數組

char buffer[1024]; 
0

變化

char * buffer; 

char buffer[MAX_PATH]; 

或類似的東西。

0

U必須爲緩衝區變量分配內存。 char * buffer;語句不分配任何內存,以便我可以有任何數據。

這麼好,你試試吧 chat buffer [// buffer length];

或char * buffer = new char(// buffer length);

或char buffer =(char)malloc(// buffer length);

這些上述語句將分配內存並修復您的問題。

相關問題