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

奇偶校验位和 fputc

如何解决奇偶校验位和 fputc

所以我正在尝试使用奇偶校验位,我计算了它们并将它们制成 p0、p1、p2。我的问题是如何在 fputc 中获取它们,因为它只能采用一个变量。

p0、p1、p2 必须通过操作放入输出文件中。但我似乎无法弄清楚如何做到这一点。有人可以帮我吗?

我的代码


int main(int argc,char *argv[])
{
    FILE *fp1,*fp2; 
    char buffer[100];

    int charFile;
    int res;

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

    int highNibble; //for getting the high nibble
    int lowNibble; //for getting the low nibble

    fp1 = fopen(argv[1],"r"); //opening the .txt file and reading it
    fp2 = fopen(argv[2],"w" ); //opening the .txt file and writing to it

    if(fp1 == 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
    }
    if(fp2 == 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(fp1); //Used to obtain the input form a file as a single character at a time.
        res = fread(buffer,sizeof(char),1,fp1);

        while(res != 0)
        {
            byte = (int) charFile;
            //get high nibble
            highNibble = (byte >> 4) & 0x0f;
            //get low nibble
            lowNibble = (byte & 0x0f);

            //d0,d3 + high nibble
            d0 = (highNibble >> 0) & 0b0001;
            d1 = (highNibble >> 1) & 0b0001;
            d2 = (highNibble >> 2) & 0b0001;
            d3 = (highNibble >> 3) & 0b0001;

            //p0,p2
            p0 = (d0 + d1 + d2) % 2;
            p1 = (d0 + d1 + d3) % 2;
            p2 = (d1 + d2 + d3) % 2;

            fputc((p0,p2),fp2);

            //do,d3 + low nibble
            d0 = (lowNibble >> 0) & 0b0001;
            d1 = (lowNibble >> 1) & 0b0001;
            d2 = (lowNibble >> 2) & 0b0001;
            d3 = (lowNibble >> 3) & 0b0001;

            //p0,p2
            p0 = (d0 + d1 + d2) % 2;
            p1 = (d0 + d1 + d3) % 2;
            p2 = (d1 + d2 + d3) % 2;
        
        }
        
    }
    fclose(fp1); //close the input file
    fclose(fp2); //close the output ifle
}

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