问题描述
所以我正在尝试使用奇偶校验位,我计算了它们并将它们制成 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 (将#修改为@)