问题描述
作为到期,我正在尝试使用文件处理和阿迪达斯。但由于某种原因,我的解码无法完全正常工作。
问题是什么:当我使用单词 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 (将#修改为@)