2维char数组在C中填充错误的输入

时间:2018-02-07 16:31:45

标签: c arrays

我编写了一个Whatsapp聊天分析器。它是用C语言编写的。首先,我的程序使用fopen()来打开导出的Whatsapp-chat; ...稍后它应该将临时数组中的Chat成员的名称保存,然后应该写入(如果end_array中的end_array中已经存在相同的名称。否则,该行将被跳过。

检测成员姓名:

- '我的名字'之间的字符:得到保存

07.11.17,14:38 - 艾伦(TFO):艾伦

问题: End_Array充满了奇怪的字符(见下图)我该如何解决这个问题?在第一行应该只是'andisville',在第二行Florian Steger ....

Problem:

姓名: Chat txt

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int printEndArr(int nl, int members, char end_arr3[nl][members]);
int clearArray(int nl, int members, char end_arr3[nl][members]);

int main()
{
   FILE *fp;
   fp = fopen("datei3.txt", "r");

   int a = 0;
   int i = 0; 
   int k = 0; 
   int c = 0; 
   int c2 = -1; 
   int j = 0;
   int members = 0; 
   int m; 
   int n,strc  = 0;
   int x,z, zus = 1, nl = 30,zl =0,zle =0,zlex = 0,firc;    
   //Don't ask me why I formatted the declarations so bad

   char chr, *statBeff; 

   scanf("%d",&members);
   char end_arr3[nl][members];
   char temp_array[nl];
   int counter[members];

   clearArray(nl,members, end_arr3);

   if(fp == NULL)
   {
      printf("Achtung die Datei existiert nicht!");//The File doesn`t exist
   }
   else
   {
      for(i = 0; chr != EOF; i++)       //Read every char of the text file
      { 
         chr = fgetc(fp);
         if(chr == '-')
         {
            chr = fgetc(fp);
            if(chr == ' ')
            {
               j=0;
               while((chr = fgetc(fp))!=':')
               {
                  temp_array[j] = chr;
                  j++;
               }

               temp_array[j] = '\0'; //The tempArray gets terminated by '\0'

               if((strstr(temp_array, "Betreff") == NULL)) //If the temporary array doesn't contain the String 'Betreff'..
               {
                  for(z=0; z < members;z++)
                  {
                     if(strcmp(end_arr3[z],temp_array)==0) //Prove if the name of the temp_array is already in the endarray
                     {
                        strc = 1;
                        a--;                
                        break;
                     }  
                  }
                  if (strc == 0)//If the temparray isn't in the end array:
                  {
                     strcpy(end_arr3[a], temp_array);
                  }
               }
               else
               {
                  a--;
               }

               strc =0;
               ++a;
               if(a == members) //If all members of the whatsapp chat are found the for-loop will be determinated
               break;
            }
         }
      }       
   }       
   printEndArr(nl,members, end_arr3); //end array gets printed

   fclose(fp);

   return 0;
}

int printEndArr(int nl, int members, char end_arr3[nl][members])
{
   int i,k;

   for(i=0;i<members;i++)
   {
      for(k=0;k<nl;k++)
      {
         printf("%c",end_arr3[i][k]);
      }
      printf("\n");
   }

   return 0;
}


int clearArray(int nl, int members, char end_arr3[nl][members])
{
   int i,k;

   for(i=0;i<members;i++)
   {
      for(k=0;k<nl;k++)
      {
         end_arr3[i][k] = ' ';
      }
   }

   return 0;
}

1 个答案:

答案 0 :(得分:1)

您已向后宣布end_arr3

char end_arr3[nl][members];

其中nl是每个字符串空间的长度members是字符串的数字。但这为nl字符串留出了空间,每个字符串的长度为members