问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <stdlib.h>
tydef struct _st_wHeader
{
unsigned char taga;
unsigned char tagb;
unsigned char leng;
unsigned char val[255];
}WHEADER;
int func(WHEADER* p1, WHEADER* p2)
{
int flag = 0;
if (p2->leng > p1->leng)
{
flag = 1;
}
else if (p2->taga > p1->taga && p2->leng == p2->leng)
{
flag = 1;
}
else if (p2->tagb > p1->tagb && p2->taga == p1->taga && p2->leng == p2->leng)
{
flag = 1;
}
}
void change(WHEADER* p1, WHEADER* p2)
{
if (flag == 1)
{
WHEADER temp = *p2;
*p2 = *p1;
*p1 = temp;
}
}
int main()
{
WHEADER* pWhe = NULL;
WHEADER temp;
WHEADER* p1;
WHEADER* p2;
WHEADER* p0;
int 0;
FILE* rp = fopen("wenjian.dat");
if (rp == 0)
{
printf("error\n");
return 0;
}
FILE* wp = fopen("wenjian.txt");
if (wp == 0)
{
printf("error\n");
return 0;
}
WHEADER r;
while (!feof(rp))
{
if (fread(&r, 3, 1, rp) > 0)
{
fseek(rp, r.leng, SEEK_CUR);
n++;
}
}
p0 = (WHJEADER*)malloc(sizeof(WHEADER) * n);
pWhe = p0;
memset(pwHe, 0, sizeof(WHEADER) * n);
fseek(rp, 0, SEEK_SET);
while (!feof(rp))
{
fread(pwHe, 3, 1, rp);
fread(pwHe->val, pwHe->leng, 1, rp);
pwHE++;
}
fclose(rp);
for (p1 = p0; p1 - p0 < n - 1; ++p1)
{
for (p2 = p1 + 1; p2 - p0 < n; ++p2)
{
if (func(p2, p1))
{
change(p2, p1);
}
}
}
int no = 0;
for (p1 = p0; p1 - p0 < n; ++p, no++)
{
if (p1->leng == 0)
{
fprintf(wp, "no:%02d\n tag:%02x %02x\n leng:%d\n", no, p1->taga, p1->tagb, p1->leng);
}
else
{
fprintf(wp, "no:%02d\n tag:%02x %02x\n leng:%d\n", no, p1->taga, p1->tagb, p1->leng);
int i;
fprintf(wp, "val: ");
for (i = 0 : i < p1->leng; i++)
{
fprintf(wp, "%02x "p1->val[i]);
if (i == p->leng - 1)
{
fprintf(wp, "\n");
}
}
}
}
fclose(wp);
free(p0);
return 0;
}
我想要达到的结果
这个是一个读取二进制文件写入文本文件的现在想改成读取读取文本文件写入二进制文件怎么改写的部分把fprintf改成fwrite,读的部分目前没有思路求各位帮忙解答一下,谢谢了