16,471
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "string"
static int global_get_index = 0;
typedef unsigned char u1;
typedef signed char i1;
typedef unsigned short u2;
typedef signed short i2;
typedef unsigned long u4;
typedef signed long i4;
inline void getstr(u1 *str, int size, FILE *f) {
fread(str,size,1,f);
}
inline void putstr(FILE *outf, int size, u1 *str) {
fwrite(str,size,1,outf);
}
inline u1 get1(FILE *f) {
return getc(f);
}
inline u2 get2(FILE *f) {
u2 t1 = get1(f); u2 t2 = get1(f);
return (u2)((t1 << 8) | t2);
}
inline u4 get4(FILE *f) {
u4 t1 = get2(f); u4 t2 = get2(f);
return (u4)((t1 << 16) | t2);
}
inline void Bswap(void *p1, void *p2, int size)
{
u1 *b1 = (u1 *)p1, *b2 = (u1 *)p2;
switch (size) {
case sizeof(u2):
b1[0] = b2[1];
b1[1] = b2[0];
break;
case sizeof(u4):
b1[0] = b2[3];
b1[1] = b2[2];
b1[2] = b2[1];
b1[3] = b2[0];
break;
}
}
inline void put4(FILE *f, u4 v) {u1 t[4]; Bswap(t, &v, 4); putstr(f, 4, t);}
int _tmain(int argc, _TCHAR* argv[])
{
FILE *infile, *outfile;
infile = fopen("1.txt", "rb");
outfile = fopen("2.txt", "wb");
u4 cafebabe = get4(infile);
put4(outfile,cafebabe);
getchar();
fclose(infile);
fclose(outfile);
return 0;
}