读取文件内容时出现无限循环BUG

做哈夫曼编码译码过程
先读取哈夫曼树
再读取哈夫曼编码进行译码,在读取第二个文件的时候发生了bug,出现了无限循环的现象
用了用了两个while(!feof())读取文件用的fgetc
其中的代码片段

if(mode=='d'){
        FILE *file1;
        file1 = fopen(argv[2],"r+");
        char HC[30][30]={{'#'}};
        int n = 0;
        int m = 0;
        char ch = fgetc(file1);
        char cha[100]={'#'};
        int i = 0;
        while(feof(file1)){
            if(ch==' '||ch=='\n'){
                ch = fgetc(file1);
            }
            else{
                if(ch<'0'||ch>'9'){
                    cha[m] = ch;
                    ch = fgetc(file1);
                }
                if(ch=='0'||ch=='1'){
                    int k = 0;
                    HC[i][k]=ch;
                    k++;
                    ch = fgetc(file1);
                    while(ch=='0'||ch=='1'){
                        HC[n][k]=ch;
                        ch = fgetc(file1);
                    }
                    HC[i][k]='\0';
                    i++;
                }
            }
        }
        FILE *file2;
        file2 = fopen(argv[3],"r+");
        FILE *file3;
        file3 = fopen(argv[4],"r+");
        printf("OK\n");
        fclose(file1);
        rewind(file2);
        ch = fgetc(file2);
        while(!feof(file2)){
            if(ch=='\n'){
                fputs("\n",file3);
                ch =fgetc(file2);
                printf("\n");
            }
            if(ch=='^'){
                fputs(" ",file3);
                ch = fgetc(file2);
                printf(" ");
            }
            cout<<ch;
            if(ch=='0'||ch=='1'){
                char a[100]={'#'};
                int k = 0;
                a[k] = ch;
                k++;
                ch = fgetc(file2);
                while(ch=='0'||ch=='1'){
                    a[k]=ch;
                    ch = fgetc(file2);
                    k++;
                }
                a[k]='\0';
                for(int index = 0 ; index < 30; index++){
                    if(str_cmp(a,HC[index])){
                        fputc(cha[index],file3);
                        cout<<cha[index];
                        index=30;
                    }
                }
            }
        }
        printf("OK\n");
        fclose(file2);
        fclose(file3);
    }

求帮忙解决

你在whilie里嵌套了while,但是在内层while中,没有判断是否已经到文件尾了。
读取文件的条件是,必须 !feof(file2)才行,所以 ch = fgetc(file2);必须放在while循环中。
如下图所示

img

代码修改如下:


if(mode=='d'){
    FILE *file1;
    file1 = fopen(argv[2],"r+");
    char HC[30][30]={{'#'}};
    int n = 0;
    int m = 0;
    char ch;// = fgetc(file1);
    char cha[100]={'#'};
    int i = 0;
    while(feof(file1))
    {
        ch = fgetc(file1);
        if(ch==' '||ch=='\n')
        {
            ch = fgetc(file1);
        }else
        {
            if(ch<'0'||ch>'9')
            {
                cha[m] = ch;
                //ch = fgetc(file1);
            }
            else if(ch=='0'||ch=='1')
            {
                int k = 0;
                HC[i][k]=ch;
                k++;
                //ch = fgetc(file1);
                while( (!feof(file1)) && (ch=='0'||ch=='1')){
                    ch = fgetc(file1);
                    HC[n][k]=ch;
                    k++;
                    //ch = fgetc(file1);
                }
                HC[i][k]='\0';
                i++;
            }
        }
    }
    FILE *file2;
    file2 = fopen(argv[3],"r+");
    FILE *file3;
    file3 = fopen(argv[4],"r+");
    printf("OK\n");
    fclose(file1);
    rewind(file2);
    while(!feof(file2)){
        ch = fgetc(file2); //读取文件放在while中,只有当while条件成立时,才能读取
        if(ch=='\n'){
            fputs("\n",file3);
            ch =fgetc(file2);
            printf("\n");
        }
        else if(ch=='^'){
            fputs(" ",file3);
            ch = fgetc(file2);
            printf(" ");
        }
        else
        {
            cout<<ch;
            if(ch=='0'||ch=='1'){
                char a[100]={'#'};
                int k = 0;
                a[k] = ch;
                k++;
                //ch = fgetc(file2);
                while( (!feof(file2)) &&  (ch=='0'||ch=='1') ){
                    ch = fgetc(file2); //读取放在while中
                    a[k]=ch;
                    //ch = fgetc(file2); //这一句注释掉
                    k++;
                }
                a[k]='\0';
                for(int index = 0 ; index < 30; index++){
                    if(str_cmp(a,HC[index])){
                        fputc(cha[index],file3);
                        cout<<cha[index];
                        index=30;
                    }
                }
            }
        }
    }//while end
    printf("OK\n");
    fclose(file2);
    fclose(file3);
}