#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,读的部分目前没有思路求各位帮忙解答一下,谢谢了
下面程序是读入你输出的文本文件,写入相应的二进制文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _st_wHeader {
unsigned char taga;
unsigned char tagb;
unsigned char leng;
unsigned char val[255];
} WHEADER;
int read(FILE *file, WHEADER *r) {
int no;
if (fscanf(file, " no:%d", &no) != 1)
return 0;
int taga, tagb;
if (fscanf(file, " tag:%x%x", &taga, &tagb) != 2)
return 0;
r->taga = taga;
r->tagb = tagb;
int leng;
if (fscanf(file, " leng:%d", &leng) != 1)
return 0;
r->leng = leng;
if (r->leng > 0) {
for (int i = 0; i < r->leng; i++) {
int val;
fscanf(file, "%x", &val);
r->val[i] = val;
}
}
return 1;
}
int main() {
const char *in_filename = "wenjian.txt";
const char *out_filename = "wenjia.dat";
FILE *in_file = fopen(in_filename, "r");
if (!in_file) {
printf("failed to open %s\n", in_filename);
return 1;
}
FILE *out_file = fopen(out_filename, "wb");
if (!out_file) {
printf("failed to open %s\n", out_filename);
fclose(in_file);
return 2;
}
WHEADER r;
while (read(in_file, &r))
fwrite(&r, sizeof(WHEADER), 1, out_file);
fclose(in_file);
fclose(out_file);
return 0;
}
代码错误较多
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <stdlib.h>
typedef struct _st_wHeader
{
unsigned char taga;
unsigned char tagb;
unsigned char leng;
unsigned char val[255];
}WHEADER;
int flag = 0;
int func(WHEADER* p1, WHEADER* p2)
{
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 n=0;
int flag=0;
FILE* rp = fopen("wenjian.dat","r");
if (rp == NULL)
{
printf("error\n");
return 0;
}
FILE* wp = fopen("wenjian.txt","w");
if (wp == NULL)
{
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 = (WHEADER*)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,p=0;
for (p1 = p0; p1 - p0 < n; ++p1, 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 == p1->leng - 1)
{
fprintf(wp, "\n");
}
}
}
}
fclose(wp);
free(p0);
return 0;
}
这个是一个读取文本文件写入二进制的现在想改成读取读取文本文件写入二进制文件
断一下句,前后不是一样吗?都是读取文本文件写入二进制啊
首先用二进制方式打开文件
#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.txt");
if (rp == 0)
{
printf("error\n");
return 0;
}
FILE* wp = fopen("wenjian.dat",wb);
if (wp == 0)
{
printf("error\n");
return 0;
}
p0 = (WHJEADER*)malloc(sizeof(WHEADER) * n);
pWhe = p0;
memset(pwHe, 0, sizeof(WHEADER) * n);
fseek(rp, 0, SEEK_SET);
int no = 0;
while (!feof(rp))
{
fscanf(rp,"no:%d tag:%x%x leng:%d",&no,&pWhe->taga,&pWhe->tagb,&p1->leng1);
if(p1->leng1>0)
{
for (i = 0 : i < p1->leng; i++)
{
if(i==0)
fscanf(rp,"val:%x",&p1->val[i]);
else
fscanf(rp,"%x",&p1->val[i]);
}
}
}
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);
}
}
}
no = 0;
for (p1 = p0; p1 - p0 < n; ++p, no++)
{
if (p1->leng == 0)
{
fwrite(&no,sizeof(int),1,wp);
fwrite(p1,sizeof(char),3,wp);
}
else
{
fwrite(&no,sizeof(int),1,wp);
fwrite(p1,sizeof(WHEADER),1,wp);
}
}
fclose(wp);
free(p0);
return 0;
}