2013-09-28 35 views
1

我創建了一個文件指針數組,並獲得核心轉儲。如果我將所有內容寫入一個文件,那麼我的程序運行良好。這是什麼原因?無法在C程序中打開16個文件(核心轉儲)

This Works。

unsigned char error_array[4][4][256] 
FILE *hypo_table; 
hypo_table = fopen("00.txt", "w"); 
for(i = 0; i < 4; i++) { 
    for(j = 0; j < 4; j++) { 
     for(hypo_key = 0; hypo_key < 256; hypo_key++) { 
      //process error_array 
      fprintf(hypo_table, "%.2x ", error_array[i][j][hypo_key]); 
      if(hypo_key == 255) 
       break; 

這不起作用(核心轉儲)。

unsigned char error_array[4][4][256] 
FILE *hypo_table[4][4]; 
hypo_table[0][0] = fopen("00.txt", "w"); 
hypo_table[1][0] = fopen("10.txt", "w"); 
hypo_table[2][0] = fopen("20.txt", "w"); 
hypo_table[3][0] = fopen("30.txt", "w"); 

hypo_table[1][0] = fopen("10.txt", "w"); 
hypo_table[1][1] = fopen("11.txt", "w"); 
hypo_table[1][2] = fopen("12.txt", "w"); 
hypo_table[1][3] = fopen("13.txt", "w"); 

hypo_table[2][0] = fopen("20.txt", "w"); 
hypo_table[2][1] = fopen("21.txt", "w"); 
hypo_table[2][2] = fopen("22.txt", "w"); 
hypo_table[2][3] = fopen("23.txt", "w"); 

hypo_table[3][0] = fopen("30.txt", "w"); 
hypo_table[3][1] = fopen("31.txt", "w"); 
hypo_table[3][2] = fopen("32.txt", "w"); 
hypo_table[3][3] = fopen("33.txt", "w"); 


for(i = 0; i < 4; i++) { 
    for(j = 0; j < 4; j++) { 
     for(hypo_key = 0; hypo_key < 256; hypo_key++) { 
      //process error_array 
      fprintf(hypo_table[i][j], "%.2x ", error_array[i][j][hypo_key]); 
      if(hypo_key == 255) 
       break; 
+0

在哪條線路上出現故障? –

+0

當i = 0時,j = 0,hypo_key = 255 – drdot

+0

那麼'error_array [i] [j] [hypo_key]''怎麼辦? – paulsm4

回答

3

您正在做的錯誤是您在循環中使用未初始化的流。您正在打開twice.But你沒有打開某些文件相同的文件和你沒有指派一些流

hypo_table[0][0] = fopen("00.txt", "w"); 

這裏你沒有打開的文件01.txt02.txt,並且03.txt
hypo_table[0][1],hypo_table[0][2],hypo_table[0][3]無效流

hypo_table[1][0] = fopen("10.txt", "w"); //Here ,it is 01 
hypo_table[2][0] = fopen("20.txt", "w"); //Here ,it is 02 
hypo_table[3][0] = fopen("30.txt", "w"); //Here ,it is 03 

hypo_table[1][0] = fopen("10.txt", "w"); //Here You are reopening 
hypo_table[1][1] = fopen("11.txt", "w"); 
hypo_table[1][2] = fopen("12.txt", "w"); 
hypo_table[1][3] = fopen("13.txt", "w"); 

for(i = 0; i < 4; i++) { 
    for(j = 0; j < 4; j++) { 
     for(hypo_key = 0; hypo_key < 256; hypo_key++) { 
      //process error_array 

這裏您試圖訪問無效的流。這會給段錯誤。

  fprintf(hypo_table[i][j], "%.2x ", error_array[i][j][hypo_key]); 

使用循環打開文件並分配流,並檢查基於返回值的fopen()返回值進一步進行。使用sprintf()創建文件名字符串。

FILE *hypo_table[4][4],*fp=NULL; 
char buf[10]; 

    for(i = 0; i < 4; i++) { 
     for(j = 0; j < 4; j++) { 
       sprintf(buf,"%d%d.txt",i,j);  
       fp = fopen(buf, "w"); 
       if(fp!=NULL) 
        { 
        hypo_table[i][j] =fp; 
        //You can include inner loop Here 
        fp=NULL; 
        }  
       else 
        { 
        perror("ERROR"); 
        //Handle As you want or simply exit. 
        } 
       } 
      } 
2

您應該認識到fopen可能會失敗的事實。可以打開一個進程的文件數量的上限。

如果fopen失敗,則返回NULL。

如果返回NULL,fprintf中將訪問違反

hypo_table[3][3] = fopen("33.txt", "w"); assert(hypo_table[3][3] != NULL); 

如果你拋在那兒斷言爲他們每個人,你會看到哪一個是失敗的。 您需要#include才能使用斷言功能。

通常,當你打開一個文件時,你應該遵循的東西沿着這個模式的行:

FILE *fp = fopen("some_file.txt", "rw"); 
if (fp != NULL) 
{ 
    /* Do file stuff */ 

    fclose(fp); 
} 
else 
{ 
    printf("golly gee wilkers, the file didn't open\n"); 
    /* and maybe look at the errno variable to figure out why */ 
} 
+0

感謝您的評論。 hypo_key是一個無符號的字符,中斷條件是爲了防止無限循環。我現在會嘗試斷言。 – drdot

+0

斷言沒有失敗...... – drdot

+0

@dannycrane它沒有失敗的原因有兩個。第一種可能性是它被打開,就好像「rw」只是「r」一樣,因爲「rw」只在Windows上以讀寫模式AFAIK打開;任何文件寫入功能都會失敗。另一種可能性是它以代碼指示的讀寫模式打開,默認模式是讀模式,直到發生文件寫入。這意味着該文件可以成功地多次打開...閱讀。寫作是另一回事。我不確定如果一個文件寫入使用一個「FILE *」發生,然後另一個寫入,則會發生什麼情況。 –

0

1)沒有理由你不應該能夠同時打開16個文件。

2)您的二維陣列hypo_table看起來沒問題。我們不知道你的3天數組error_array。我懷疑這是你的問題所在。

4)你一定要檢查所有可能的錯誤條件 - 比如「fopen()」失敗。

5)你應該通過一個調試器下的代碼肯定步驟 - 這將幫助你確定究竟其中它的失敗,並爲什麼

+0

感謝您的評論。我添加了更多的細節。 – drdot