关于读二进制文件写入二进制文件的问题,结构体是固定的十六个数据总共想要一次性把这十六个全读完, 并且fseek第二个参数偏移量是结构体成员 plen,越过plen才能读下一个结构体,写了一下代码,有错误,但不知道应该怎么改,请各位帮忙看一下改一下
#include
#include
#include
typedef struct st_qwe
{
unsigned header;
unsigned version;
unsigned plen[2];
unsigned song[2];
unsigned shou[2];
unsigned ID[2];
unsigned luyou[2];
unsigned bao[4];
}QWE;
int main() {
QWE* pqwe=NULL;
QWE* p1;
QWE* p2;
QWE* p0;
QWE r;
int count=0;
FILE* pfBIn1=fopen("E:\\qwe_cmds.bin",rb);
if(pfBIn1==NULL)
{
printf("error");
return 0;
}
FILE* pfBIn2=fopen("E:\\abc.bin",w);
if(pfBIn1==NULL)
{
printf("error");
return 0;
}
while(!feof(pfBIn1))
{
if(fread(&r,16,1,pfBIn1)>0)
{
fseek(pfBIn1,r.plen[1],SEEk_CUR);
count++;
}
}
p0=(QWE*)malloc(count*sizeof(QWE));
pqwe=p0;
memset=(pqwe,0,count*sizeof(QWE));
fseek(pfBIn1,0,SEEK_SET);
while(!feof(pfBIn1))
{
fread(pqwe,16,1,pfBIn1);
pqwe++;
}
return 0;
}
表面上的编译错误改正:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct st_qwe
{
unsigned header;
unsigned version;
unsigned plen[2];
unsigned song[2];
unsigned shou[2];
unsigned ID[2];
unsigned luyou[2];
unsigned bao[4];
}QWE;
int main() {
QWE* pqwe = NULL;
QWE* p1;
QWE* p2;
QWE* p0;
QWE r;
int count = 0;
FILE* pfBIn1 = fopen("E:\\qwe_cmds.bin", "rb");
if (pfBIn1 == NULL)
{
printf("error");
return 0;
}
FILE* pfBIn2 = fopen("E:\\abc.bin", "w");
if (pfBIn1 == NULL)
{
printf("error");
return 0;
}
while (!feof(pfBIn1))
{
if (fread(&r, 16, 1, pfBIn1)>0)
{
fseek(pfBIn1, r.plen[1], SEEK_CUR);
count++;
}
}
p0 = (QWE*)malloc(count * sizeof(QWE));
pqwe = p0;
memset(pqwe, 0, count * sizeof(QWE));
fseek(pfBIn1, 0, SEEK_SET);
while (!feof(pfBIn1))
{
fread(pqwe, 16, 1, pfBIn1);
pqwe++;
}
return 0;
}
贴一下你的报错信息,方便debug分析
错误纠正:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct st_qwe
{
unsigned header;
unsigned version;
unsigned plen[2];
unsigned song[2];
unsigned shou[2];
unsigned ID[2];
unsigned luyou[2];
unsigned bao[4];
}QWE;
int main() {
QWE* pqwe = NULL;
QWE* p1;
QWE* p2;
QWE* p0;
QWE r;
int count = 0;
FILE* pfBIn1 = fopen("E:\\qwe_cmds.bin", "rb");
if (pfBIn1 == NULL)
{
printf("error");
return 0;
}
FILE* pfBIn2 = fopen("E:\\abc.bin", "w");
if (pfBIn1 == NULL)
{
printf("error");
return 0;
}
while (!feof(pfBIn1))
{
if (fread(&r, 16, 1, pfBIn1)>0)
{
fseek(pfBIn1, r.plen[1], SEEK_CUR);
count++;
}
}
p0 = (QWE*)malloc(count * sizeof(QWE));
pqwe = p0;
memset(pqwe, 0, count * sizeof(QWE));
fseek(pfBIn1, 0, SEEK_SET);
while (!feof(pfBIn1))
{
fread(pqwe, 16, 1, pfBIn1);
pqwe++;
}
return 0;
}