struct data { u_short sin_port; struct in_addr sin_addr; unsigned char id; }; struct data Data[141]; char *buf,*buf1, buf1 = (char *)malloc(10000); memset(buf1, 0, 10000); for (i =0;i< 141;i++) { memcpy(buf1, &Data[i].id,20); printf("buf11: %x
", buf1); buf1+=20; memcpy(buf1, &Data[i].sin_addr, 4); buf1+=4; memcpy(buf1, &Data[i].sin_port, 2); buf1+=2; size1 += 26; } buf1 -= size1; write(fd2, buf1, size1+1); close(fd2); free(buf1);
我发现这样写入的数据总是不对/?请教哪里有问题啊?一般都是先写入buffer,再写入文件?
不要这么麻烦。你顶一个struct的指针,指向"buf指针强转型后的位置",然后将你要copy的struct赋值过来即可。
char* buf = new char[100];
struct Data d1; // init d1
struct Data d2 = (struct Data)buf;
*d2 = d1;
一周热门 更多>