Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C数组打印不正确,超出循环_C_Arrays_Loops - Fatal编程技术网

C数组打印不正确,超出循环

C数组打印不正确,超出循环,c,arrays,loops,C,Arrays,Loops,我遇到一个问题,当我的数组在用于从文件中读取内容的循环之外时,它将无法正确打印。代码如下所示: 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 l

我遇到一个问题,当我的数组在用于从文件中读取内容的循环之外时,它将无法正确打印。代码如下所示:

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)
}
int main(int argc,char**argv)
{
char*fileArray[78];
int i,j;
DIR*d;
结构方向*dir;
查阿拉[128][128];
char*arra1[14284][128];
字符行[1024];
字符行1[1024];
字符噪声列表[128][128];
char*noiseList1[14][128];
字符*令牌;
字符*1;
char*path=“./alerts2013/2013/”;
char*path1=“./Noise_和_Concepts/”;
字符*文件名;
char*fileName1;
字符seps[]=“,\t\n”;
文件*myfile;
文件*噪音;
int size=0;
d=opendir(“./alerts2013/2013”);
fileName1=stradd(路径1,“noise.txt”);
//printf(“%s\n”,fileName1);
噪音=fopen(文件名1,“r”);
如果(噪声==0)
{
printf(“无法打开文件\n”);
出口(1);
}
INTA,b;
对于(a=0;a<128;a++){
第[a]行=“\0”;
}
对于(a=0;a<128;a++){
对于(b=0;b<128;b++){
噪音列表[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;
INTR;
while(fgets(line,sizeof(line),noise)!=NULL){
strcpy(噪声列表[k],行);
//printf(“%s”,噪声列表[k]);
令牌=strtok(noiseList[k],seps);
while(令牌!=NULL)
{
/*虽然“字符串”中有标记*/
//printf(“%s\n”,标记);
//printf(“数组-->%s\n”,令牌);
下_字符串(令牌);
noiseList1[n][0]=令牌;
n++;
/*获取下一个令牌:*/
令牌=strtok(空,seps);
}
k++;
}
如果(d)
{
而((dir=readdir(d))!=NULL)
{
fileArray[i]=dir->d_name;
//printf(“%s\n”,fileArray[i]);
fileName=stradd(路径,目录->文件名);
//printf(“%s\n”,文件名);
免费(文件名);
myfile=fopen(文件名,“r”);
如果(myfile==0)
{
printf(“无法打开文件\n”);
出口(1);
}
对于(i=0;i<128;i++){
第1行[i]='\0';
}
如果(myfile!=NULL){
while(fgets(line1,sizeof(line1),myfile)!=NULL){
strcpy(arra[l],第1行);
//printf(“令牌:\n”);
/*建立字符串并获取第一个令牌:*/
令牌=strtok(arra[l],seps);
while(令牌!=NULL)
{
/*虽然“字符串”中有标记*/
//printf(“%s\n”,标记);
//printf(“数组-->%s\n”,令牌);
下_字符串(令牌);
arra1[m][0]=令牌;
printf(“Arra1:%s\n”,Arra1[m][0]);//打印
//在这里正确
大小++;
m++;
/*获取下一个令牌:*/
令牌=strtok(空,seps);
}
//printf(“数组-->%s,&arra[i]);
i++;
}
}
fclose(myfile);
i++;
}
closedir(d);
}
INTP;
int w;
printf(“%d\n”,大小);
/*对于(p=0;p<14;p++){
printf(“%s\n”,noiseList1[p][0]);
}*/
对于(w=0;w

在第一个printf语句notin comments时,数组将正确打印。但是,在代码底部的for循环not in comments中,它切掉了数组中字符串的一些字母。例如,未经身份验证可能变成未经身份验证。我不知道为什么会这样。我认为我的数组没有正确保存,但我不能完全确定。我该怎么做呢?

嗯,这是我见过的最辣的意大利面


char*arra1[14284][128]创建(14284*128)字符指针,而不是14284个单独的128个字符串。我觉得你不是有意的

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

我不是100%认为您可以使用
strtok
无限期返回的指针。这个指针似乎是
arra[l]
的一部分,它似乎被while循环破坏了。我甚至没有看到
l
增加!如果您增加该程序,它可能会工作

我认为您真正想要做的是创建14284个字符串,如下所示:
chararra1[14284][128]


然后,当您在每次
strtok
之后获得
token
时,您应该
strcpy(arra1[m],token)
char*arra1[14284][128] : 这似乎是一个很大的堆栈,因为我需要逐字读取大量的文件。当我运行计数器查看总共有多少个单词时,弹出14284个;myfile=fopen(文件名,“r”)
是个坏消息,不管周围发生了什么。我没有足够的代表对你的帖子发表评论,所以我会对我的帖子发表评论-我强烈建议你阅读斯坦福大学的文章,了解[]和*操作符以及它们是如何