2016-11-06 140 views
0

假設我想「重用」一個char指針數組,像在下面的程序中循環參數列表中給出的文件,循環文件中的行,將它們添加到動態分配的數組中,然後打印它:重用char指針數組

// includes 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

// globals 
int progReturn = 0; 
int globalLineCounter = 0; 



////// main 
int main(int argc, char *argv[]) { 
    FILE *fp; 
    int i; 


    // iterate files. first arg is the program name 
    for (i = 1; i < argc; i++) { 
     fp = fopen(argv[i], "r"); 
     if (fp == NULL) { 
      fprintf(stderr, "The file '%s' did not exist.\n", argv[i]); 
      progReturn = 1; 

     } else { 

      // read lines from the file 
      char line[256]; 

      // THE PROBLEM: I'd like to completely clear this array.    
      char **lines = malloc(16 * sizeof(char*)); 

      // iterate lines 
      int fileLineCounter = 0; 
      while (fgets(line, sizeof(line), fp)) { 

       // remove newline 
       strtok(line, "\n"); 

       // add lines to array 
       lines[globalLineCounter] = malloc(256 * sizeof(char)); 
       strcpy(lines[globalLineCounter], line); 
       //printf("%s\n", lines[globalLineCounter]); // tester 

       fileLineCounter++; 
       globalLineCounter++;     
      } 
      // all lines read 
      printf("The file '%s' had %d lines.\n", argv[i], fileLineCounter); 

      // print the array 
      int j=0; 
      for (j=0; j<fileLineCounter; j++) { 
       // PROBLEM: Garbage from the second file when it prints here. 
       printf("%s\n", lines[j]); 
      } 

      // delete lines, delete file 
      memset(lines, 0, sizeof(*lines)); 
      fclose(fp); 
     } 
    } 
    // all files read 

    return progReturn; 
} 

在第一個文件,一切正常,沒有問題。在第二個文件中,當我打印數組時,它顯示了不可打印的字符以及第一個文件中的一些行。

什麼可能導致此問題?我沒有完全清除**lines


編輯:例如輸入和輸出:

輸入文件foo:

This is a test 
of the lineSort program 
in order to 
test its capabilities. 
Lots of whitespace too! 
aaa 

bbb 

     cccccc 



aaa 
ggggg 
hhhhh 
fffff 
eeeee 
ddddd 
ppppp 

輸入文件欄:

aaaaaaaaaaaaaaaaa 
bbbbbbbbbbbbbbbbb 
zzzzzzzzzzzzzzzzz 
ccccccccccccccccc 
yyyyyyyyyyyyyyyyy 

輸出sortLine foo bar

The file 'foo' had 20 lines. 





     cccccc 
Lots of whitespace too! 
This is a test 
aaa 
aaa 
bbb 
ddddd 
eeeee 
fffff 
ggggg 
hhhhh 
in order to 
of the lineSort program 
ppppp 
test its capabilities. 
The file 'bar' had 5 lines. 
(x▒▒ 
(x▒▒ 
Lots of whitespace too! 
in order to 
test its capabilities. 
+0

你不會在那裏重複使用任何東西。 – melpomene

+0

無關挑剔:'的sizeof(char)的''是通過定義1',所以通過將其相乘是不必要的。 – Arkku

+0

http://stackoverflow.com/help/mcve – melpomene

回答

1
strcpy(lines[globalLineCounter], line); 

這看起來像你的主要問題。所有輸入文件中的globalLineCounter都在不斷增加。

假設您的第一個輸入文件包含10行,第二個文件包含5行。然後代碼將創建一個動態陣列(動態數組),並從所述第一文件中的行存儲在元素0 .. 9(然後將其打印)。你永遠不會釋放任何分配的內存,所以它在循環結束時都會泄漏。

對於第二個文件,您創建另一個動態數組。你存儲從要素10 .. 14(經由globalLineCounter)所述第二文件中的5條線,但然後打印元件0 .. 4fileLineCounter)。這些元素未初始化幷包含垃圾。

+0

我只能說...是啊!這解決了它。非常感謝你。 – chakeda

1
  1. char **lines初始化移到for循環的外部。
  2. 將索引計數器i重命名爲不同。
  3. 對多個文件反覆調用lines[i] = malloc(...)會導致內存泄漏。考慮在for循環內使用free,或者將這部分初始化移動到for循環之外。
+0

這並沒有真正解決這個問題。 – melpomene

+0

這可能或可能不值得進行聊天。我無法真正符合我在這個評論領域想說的話。 –