在C中,二维字符数组被错误的输入填充
我为Whatsapp聊天分析仪编程。它是用C编写的。首先,我的程序用fopen()打开exportet Whatsapp聊天室;。。。稍后,它应该将聊天室成员的姓名保存在一个临时数组中,然后将他们写入(如果相同的姓名“尚未在end_数组中”)end_数组中。否则,将跳过该行 检测成员姓名:在C中,二维字符数组被错误的输入填充,c,arrays,C,Arrays,我为Whatsapp聊天分析仪编程。它是用C编写的。首先,我的程序用fopen()打开exportet Whatsapp聊天室;。。。稍后,它应该将聊天室成员的姓名保存在一个临时数组中,然后将他们写入(如果相同的姓名“尚未在end_数组中”)end_数组中。否则,将跳过该行 检测成员姓名: #include <stdio.h> #include <stdlib.h> #include <string.h> int printEndArr(int nl, in
#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;
}
“我的名字”之间的字符:保存
07.11.1714:38-艾伦(TFO):艾伦
问题:End\u数组被奇怪的字符填满(见下图),我如何解决这个问题?第一行只能是“andisville”,第二行是Florian Steger
名称:
代码:
#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;
}
#包括
#包括
#包括
int printEndArr(int nl,int members,char end_arr3[nl][members]);
int clearArray(int nl、int成员、char end_arr3[nl][members]);
int main()
{
文件*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成员=0;
int m;
int n,strc=0;
intx,z,zus=1,nl=30,zl=0,zle=0,zlex=0,firc;
//别问我为什么我把声明格式化得这么糟糕
char-chr,*statBeff;
scanf(“%d”和成员);
char end_arr3[nl][members];
字符临时数组[nl];
国际计数器[成员];
clearArray(nl、成员、end_arr3);
如果(fp==NULL)
{
printf(“Achtung die Datei existiert nicht!”;//文件不存在
}
其他的
{
for(i=0;chr!=EOF;i++)//读取文本文件的每个字符
{
chr=fgetc(fp);
如果(chr=='-')
{
chr=fgetc(fp);
如果(chr='')
{
j=0;
而((chr=fgetc(fp))!=':')
{
temp_数组[j]=chr;
j++;
}
temp_数组[j]='\0';//temp数组以'\0'终止
if((strstrstr(temp_数组,“Betreff”)==NULL))//如果临时数组不包含字符串“Betreff”。。
{
对于(z=0;z 对于(i=0;i您已声明end\u arr3
向后:
char end_arr3[nl][members];
其中,nl
是每个字符串的空格长度,members
是字符串的数量。但是这为nl
每个长度成员的字符串留出了空格您最近没有发布相同的问题吗?我删除了该线程,打开了一个新线程。与您的问题无关,但返回了一个e> int
。当你检查EOF
时,这一点实际上很重要。这不是真正的工作方式。你应该改为编辑你的旧问题。反复发布同一个问题会让你在一段时间后被禁止。注意这一点。此外,你的问题是离题的:发布尽可能多的代码,并期望其他人为你调试它不是很好。a好得多。chr!=EOF
是错误的:你在事后测试EOF。你必须测试if((chr=fgetc(fp))!=EOF)
好的,我已经做了,现在第一行是正确的,其余的行和以前一样