gpt4 book ai didi

c - 使用位操作解码文件

转载 作者:行者123 更新时间:2023-11-30 17:28:06 25 4
gpt4 key购买 nike

不断出现段错误,我无法解决这个问题。如果程序运行“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);

}

最佳答案

while (c > 0){
...
c++;
}

这将导致无限循环。段错误来自于读取符号[4],这是一种访问冲突。

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

关于c - 使用位操作解码文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26130156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com