C 用位操作解码文件

C 用位操作解码文件,c,binary,bit-manipulation,decoding,bit-shift,C,Binary,Bit Manipulation,Decoding,Bit Shift,不断出现分割错误,我无法解决这个问题。如果程序运行“encodeFile”(编码文件)功能,则程序应能够逐个字符读取输入文件,并将字符压缩为2位值。然后,这些值将打印在输出文件中。我对这种语言很陌生。如何解决此任务 //The function void encodeFile(char *fpInputfile,char *fpOutputfile) { FILE *fileInput = fopen(fpInputfile, "r"); FILE *fileOu

不断出现分割错误,我无法解决这个问题。如果程序运行“encodeFile”(编码文件)功能,则程序应能够逐个字符读取输入文件,并将字符压缩为2位值。然后,这些值将打印在输出文件中。我对这种语言很陌生。如何解决此任务

//The function
void encodeFile(char *fpInputfile,char *fpOutputfile)
{
        FILE *fileInput = fopen(fpInputfile, "r");
        FILE *fileOutput = fopen(fpOutputfile, "w");

        if (!fileInput || !fileOutput){
            printf("ERROR MESSAGE: can not open the selected file \n");
            exit(1);
        }

        char symbols[4];
        char encodeB[4] = {0x00, 0x01, 0x02, 0x03};
        size_t c = fread(symbols, sizeof(char), 4, fileInput);

        while (c != 0){
            int i = 0;
            int j = 0;
            char temp = 0;
            while (c > 0){

                if (symbols[j] == ' '){
                    temp = encodeB[0];
                }
                else if (symbols[j] == ':'){
                    temp = encodeB[1];
                }
                else if (symbols[j] == '@'){
                    temp = encodeB[2];
                }
                else if (symbols[j] == '\n'){
                    temp = encodeB[3];
                }
                else{
                }
                j++;
                i |= temp << (c *2);
                c++;
            }
            //c = fread(symbols, sizeof(char), 4, fileInput);
            //fwrite(&temp, 1, 1, fileOutput);
        }
        fclose(fileInput);
        fclose(fileOutput);

    }
//函数
void encodeFile(char*fpInputfile,char*fpOutputfile)
{
FILE*fileInput=fopen(fpInputfile,“r”);
FILE*fileOutput=fopen(fpOutputfile,“w”);
如果(!fileInput | |!fileOutput){
printf(“错误消息:无法打开所选文件\n”);
出口(1);
}
字符符号[4];
char encodeB[4]={0x00,0x01,0x02,0x03};
size_t c=fread(符号,sizeof(char),4,fileInput);
而(c!=0){
int i=0;
int j=0;
字符温度=0;
而(c>0){
如果(符号[j]=''){
temp=encodeB[0];
}
else if(符号[j]==':'){
temp=encodeB[1];
}
else if(符号[j]='@'){
temp=encodeB[2];
}
else if(符号[j]='\n'){
temp=encodeB[3];
}
否则{
}
j++;
i |=温度
这将导致无限循环。分段错误来自读取符号[4],这是访问冲突


如果您想使用这种语言,我强烈建议您学习如何在调试器中单步执行代码。祝您好运!

我不明白您在做什么,但我认为您应该使用
c-->
而不是
c++;
更具体地说,访问冲突和随后的分段错误是由于试图读取内存而不是内存渴望程序,在这种情况下,通过尝试读取一个假定的
符号元素
,它实际上已经超过了数组的末尾。谢谢,我找到了答案:)
while (c > 0){
   ...
   c++;
}