微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解码后没有完整的单词

如何解决解码后没有完整的单词

作为到期,我正在尝试使用文件处理和阿迪达斯。但由于某种原因,我的解码无法完全正常工作。

问题是什么:当我使用单词 ferret 作为输入时,对其进行编码、通道和解码,输出单词 ferret。一旦我在里面放了另一个词,解码后它就不会完全出来。例如吉他,在我执行相同的步骤后,guatar 将退出。有人可能知道我如何让它工作吗?

这是我的代码

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    FILE *inputFile,*outputFile;

    uint8_t charFile;
    uint8_t highNibble;

    int i = 1;

    uint8_t byte;
    uint8_t d0,d1,d2,d3;
    uint8_t p0,p1,p2;

    uint8_t finalByteParity = 0;


    int nibble; //for getting the high nibble


    if (argc == 3)
    {
        printf("The first argument is %s\n",argv[0]);
        printf("The first argument is %s\n",argv[1]);
        printf("The first argument is %s\n",argv[2]);
        inputFile = fopen(argv[1],"r");  //opening the .txt file and reading it
        outputFile = fopen(argv[2],"w"); //opening the .txt file and writing to it

        if (inputFile == NULL)
        {
            printf("There has been a problem while opening the file %s\n",argv[1]); //show this when a error occus while opening the .txt
        }
        else if (outputFile == NULL)
        {
            printf("There has been a problem while writing to the file %s\n",argv[2]); //show this when a error occus while opening the .txt
        }
        else
        {
            charFile = fgetc(inputFile); //Used to obtain the input form a file as a single character at a time.
            while (!feof(inputFile)) //Found this function on the internet since the res != 0 didn't work. This while means the following: "While we haven't tried to read past end of the inputFile" it keeps going till all the bytes are read.
            {
                byte = (int)charFile;
                //get nibble
                nibble = (byte >> 3) & 0x07; //set the first byte on 0

                //d0,d3 + nibble
                d0 = (nibble >> 0) & 0b0001;
                d1 = (nibble >> 1) & 0b0001;
                d2 = (nibble >> 2) & 0b0001;
                d3 = (nibble >> 3) & 0b0001;
                //p0,p2
                p0 = (d0 + d1 + d2) % 2;
                p1 = (d0 + d1 + d3) % 2;
                p2 = (d1 + d2 + d3) % 2;

                // Add p0
                finalByteParity = p0 << 0;
                // Add p1
                finalByteParity |= p1 << 1;
                // Add p2
                finalByteParity |= p2 << 2;

              
                uint8_t checkByteParity = byte & 0x07;
                uint8_t checkFinalParity = finalByteParity & 0x07;

                int fullCheck = checkByteParity ^ checkFinalParity;

                if(fullCheck == 7)
                {
                    nibble ^= 0x02; //the wrong bit gets flipped back to the correct one
                }
                if(fullCheck == 3)
                {
                    nibble ^= 0x01;
                }
                if(fullCheck == 5)
                {
                    nibble ^= 0x04;
                }
                if(fullCheck == 6)
                {
                    nibble ^= 0x08;
                }

                if (i % 2 == 0) 
                {
                    uint8_t restoredByte =  highNibble << 4 | nibble;
                    fputc(restoredByte,outputFile);
                } 
                else 
                {
                    highNibble = nibble;                                    
                }                                                                                                                   
                charFile = fgetc(inputFile);
                i++;

            }
        }
       
        fclose(inputFile);  //close the input file
        fclose(outputFile); //close the output ifle
    }
    else
    {
        printf("you need to give 3 arguments");
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。