我遇到了一个问题,即在用于从文件中读取内容的循环外部时,我的数组不能正确打印。代码如下所示: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循环中没有注释,它会截断数组中字符串的一些字母。例如,未认证可能会成为认证。我不知道为什么会发生这种情况。我不认为我的数组正在保存正确,但我不完全确定。我将如何解决这个问题?
char * arra1 [14284] [128];':这似乎很大,以确保在堆栈上。 – BLUEPIXY
这是很大的,因为我需要逐字阅读文件的数量。当我跑完柜台看到总共有多少个单词时,14284被弹出。 – zackster7171
'free(fileName); myfile = fopen(fileName,“r”);'不管它是什么都是坏消息。 – aschepler