问题遇到的现象和发生背景
目前这个代码while循环 while (!feof(pfBin1))总是死循环,能不能在不报错的情况下改成继续往下处理读写
遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct st_ocp
{
unsigned char header;
unsigned char version;
unsigned char plen[2];
unsigned char song[2];
unsigned char shou[2];
unsigned char ID[2];
unsigned char luyou[2];
unsigned char bao[4];
}OCP;
int main() {
OCP *poCp = NULL;
OCP r;
poCp = &r;
short pLen;
FILE *pfBin1 = fopen("C:\\ocp\\ocp_cmds.bin", "rb+");//读文件
if (pfBin1 == NULL) {
printf(" error \n");
return 0;
}
FILE *pfBin2 = fopen("C:\\ocp\\tlv.bin", "w");//写文件
if (pfBin2 == NULL) {
printf(" error \n");
return 0;
}
while (!feof(pfBin1)) {
fread(&r, 16, 1, pfBin1);//先读前十六个
if ((r.song[0] == 0x04 && r.song[1] == 0x01) && (r.shou[0] == '\x2C' && r.shou[1] == 0x01))//判断
{
pLen = r.plen[0] * 256 + r.plen[1];
unsigned char *pld = NULL;
pld = (unsigned char *) malloc(sizeof(char) * pLen);
memset(pld, 0, sizeof(unsigned char) * pLen);
fread(pld, pLen, 1, pfBin1);
int ph = 0;//设游标ph
while (ph <= pLen)
{
if (pld[ph + 0] == 0x00 && pld[ph + 1] == 0x28 || pld[ph + 1] == 0x29)
{
fwrite(&pld[ph + 3], pld[ph + 2], 1, pfBin2);
}
ph = pld[ph + 2] + ph + 3;
}
free(pld);//释放内存
} else {
fseek(pfBin1, pLen, SEEK_CUR);/
}
}
fclose(pfBin2);
fclose(pfBin1);//关闭文件
getchar();
getchar();
return 0;
}
运行结果及详细报错内容
我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我的思路是在 while (ph <= pLen)加上 fread(&r, 16, 1, pfBin1);fread(pld, pLen, 1, pfBin1);可是没用
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct st_ocp
{
unsigned char header;
unsigned char version;
unsigned char plen[2];
unsigned char song[2];
unsigned char shou[2];
unsigned char ID[2];
unsigned char luyou[2];
unsigned char bao[4];
}OCP;
int main() {
OCP *poCp = NULL;
OCP r;
poCp = &r;
short pLen;
FILE *pfBin1 = fopen("C:\\ocp\\ocp_cmds.bin", "rb+");//读文件
if (pfBin1 == NULL) {
printf(" error \n");
return 0;
}
FILE *pfBin2 = fopen("C:\\ocp\\tlv.bin", "w");//写文件
if (pfBin2 == NULL) {
printf(" error \n");
return 0;
}
while (!feof(pfBin1)) {
fread(&r, 16, 1, pfBin1);//先读OCP前十六个
if ((r.song[0] == 0x04 && r.song[1] == 0x01) && (r.shou[0] == '\x2C' && r.shou[1] == 0x01))//判断
{
pLen = r.plen[0] * 256 + r.plen[1];
unsigned char *pld = NULL;
pld = (unsigned char *) malloc(sizeof(char) * pLen);
memset(pld, 0, sizeof(unsigned char) * pLen);//申请payload大小内存并清零
fread(pld, pLen, 1, pfBin1);//读文件一个OCP读payload大小
int ph = 0;//设游标ph
while (ph <= pLen)//游标在payload范围里面时
{
if (pld[ph + 0] == 0x00 && pld[ph + 1] == 0x28 || pld[ph + 1] == 0x29)
{
fwrite(&pld[ph + 3], pld[ph + 2], 1, pfBin2);
}
ph = pld[ph + 2] + ph + 3;
}
fread(&r, 16, 1, pfBin1);
fread(pld, pLen, 1, pfBin1);
free(pld);//释放内存
} else {
fseek(pfBin1, pLen, SEEK_CUR);
fread(&r, 16, 1, pfBin1);
}
}
fclose(pfBin2);
fclose(pfBin1);//关闭文件
getchar();
getchar();
return 0;
}