2017-10-15 48 views
0

我遇到了一个问题,即在用于从文件中读取内容的循环外部时,我的数组不能正确打印。代码如下所示:C阵列正确地在循环外打印

int main(int argc, char **argv) 
{ 
    char *fileArray[78]; 
    int i, j; 
    DIR *d; 
    struct dirent *dir; 
    char arra[128][128]; 
    char *arra1[14284][128]; 
    char line[1024]; 
    char line1[1024]; 
    char noiseList[128][128]; 
    char *noiseList1[14][128]; 
    char *token; 
    char *token1; 
    char *path = "./alerts2013/2013/"; 
    char *path1 = "./Noise_and_Concepts/"; 
    char *fileName; 
    char *fileName1; 
    char seps[] = " ,\t\n"; 
    FILE *myfile; 
    FILE *noise; 
    int size = 0; 
    d = opendir("./alerts2013/2013"); 
    fileName1 = stradd(path1, "noise.txt"); 
    //printf("%s\n", fileName1); 
    noise = fopen(fileName1, "r"); 
    if (noise == 0) 
    { 
     printf("can not open file \n"); 
     exit(1); 
    } 
    int a, b; 
    for(a = 0; a < 128; a++) { 
     line[a] = '\0'; 
    } 

    for(a = 0; a < 128; a++) { 
     for(b = 0; b < 128; b++) { 
      noiseList[a][b] = '\0'; 
      noiseList1[a][b] = '\0'; 
      arra[a][b] = '\0'; 
      arra1[a][b] = '\0'; 
     } 
    } 
    i = 0; 
    j = 0; 
    int k = 0; 
    int l = 0; 
    int m = 0; 
    int n = 0; 
    int q; 
    int r; 
    while(fgets(line, sizeof(line), noise) != NULL) { 
     strcpy(noiseList[k], line); 
     //printf("%s", noiseList[k]); 
     token = strtok(noiseList[k], seps); 
     while(token != NULL) 
     { 
      /* While there are tokens in "string" */ 
      //printf("%s\n", token); 
      //printf("array ----> %s\n", token); 

      lower_string(token); 
      noiseList1[n][0] = token; 
      n++; 
      /* Get next token: */ 
      token = strtok(NULL, seps); 

      } 
      k++; 
     } 

     if (d) 
     { 
      while ((dir = readdir(d)) != NULL) 
      { 
       fileArray[i] = dir->d_name; 
       //printf("%s\n", fileArray[i]); 
       fileName = stradd(path, dir->d_name); 
       //printf("%s\n", fileName); 
       free(fileName); 
       myfile = fopen(fileName,"r"); 
       if (myfile == 0) 
       { 
        printf("can not open file \n"); 
        exit(1); 
       } 

       for(i = 0; i < 128; i++) { 
        line1[i] = '\0'; 
       } 

       if(myfile != NULL) { 
         while(fgets(line1, sizeof(line1), myfile) != NULL) { 
         strcpy(arra[l], line1); 
         //printf("Tokens:\n"); 
         /* Establish string and get the first token: */ 
         token = strtok(arra[l], seps); 
         while(token != NULL) 
         { 
          /* While there are tokens in "string" */ 
          //printf("%s\n", token); 
          //printf("array ----> %s\n", token); 

          lower_string(token); 
          arra1[m][0] = token; 
          printf("Arra1: %s\n", arra1[m][0]); //PRINTING 
                  //CORRECTLY HERE 
          size++; 
          m++; 
          /* Get next token: */ 
          token = strtok(NULL, seps); 

         } 


         //printf("array ----> %s ", &arra[i]); 
         i++; 

       } 
       } 

      fclose(myfile); 


      i++; 
      } 

      closedir(d); 
    } 

    int p; 
    int w; 
    printf("%d\n", size); 
    /*for(p = 0; p < 14; p++) { 
     printf("%s\n", noiseList1[p][0]); 
    }*/ 
    for(w = 0; w < size; w++) { //PRINTING INCORRECTLY HERE 
     printf("Arr1 (final): %s\n", arra1[w][0]); 
    } 

    fclose(noise); 
    return(0) 
} 

在第一次printf语句中没有注释,数组打印正确。但是,在代码底部的for循环中没有注释,它会截断数组中字符串的一些字母。例如,未认证可能会成为认证。我不知道为什么会发生这种情况。我不认为我的数组正在保存正确,但我不完全确定。我将如何解决这个问题?

+0

char * arra1 [14284] [128];':这似乎很大,以确保在堆栈上。 – BLUEPIXY

+0

这是很大的,因为我需要逐字阅读文件的数量。当我跑完柜台看到总共有多少个单词时,14284被弹出。 – zackster7171

+1

'free(fileName); myfile = fopen(fileName,“r”);'不管它是什么都是坏消息。 – aschepler

回答

1

mm这是我见过的最热门的面食!

char *arra1[14284][128];创建(14284 * 128)个字符指针,而不是14284个独立的128个字符的字符串。我知道你不打算这么做。

但无论如何,strtok返回一个指针,然后您将其指定给[m] [0] th指针arra1。你从不使用对于arra1中[m]的每个值有127个指针。

我不是100%,你可以使用strtok无限期返回的指针。它看起来像指针是arra[l]的一部分,它似乎被一个while循环屠杀。我甚至没有看到l递增! 该程序可能会工作,如果你增加它

我觉得你真的是做的是创造14284个字符串是这样的: ​​

那么,当你得到你的token每个strtok后,你应该strcpy(arra1[m], token)

+0

我没有足够的代表评论你的帖子,所以我会评论我的 - 我强烈建议你阅读斯坦福大学的[The Essential C](http://cslibrary.stanford.edu/101/EssentialC.pdf)并理解[]和*运算符以及它们如何互换。 其他用户对* arra1 [14284] [128]的评价不是你可以在堆栈上保护的东西 - 我不知道。编译器和操作系统应该知道.data/.bss部分有多大,所以我认为如果必须的话,它会引发一个描述性错误。学习如何使用'malloc()'和'realloc()' –

+0

谢谢!将“arra1 [m] [0] = token”改为“strcpy(arra1 [m],token)”工作! – zackster7171

+0

您可以将'arra1'的类型从'char *'更改为'char'作为后代。此外,只要你保护'array1 [m]'不被显式修改,你就可以清除你创建它所用的所有变量,如果它们没有跟踪其他重要的东西的话。记住这一点,您可以减少程序的空间。编辑:也看'strcpy_s()' - 更安全 –