過于結(jié)構(gòu)體,存進(jìn)char *的buff,再寫入文件 財(cái)富值67

科技 未結(jié) 6 635
希望希望111
希望希望111 2022-09-20 20:42

struct data { u_short sin_port; structin_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);

我發(fā)現(xiàn)這樣寫入的數(shù)據(jù)總是不對/?請教哪里有問題???一般都是先寫入buffer,再寫入文件?

6條回答
  •  125jzdh
    125jzdh (樓主)
    2022-09-20 21:03

    buf1是字符串類型,應(yīng)該有結(jié)束符‘0’來收尾,否則會(huì)有亂碼。

提交回復(fù)