关于Linux C++下,将float型,long型,double型写入文件
想将float型,long型,double型等数据类型写入文件,如下程序所示,理想中的结果应该是这样:
hello word!just do it!3.123445,但是运行该代码,后面的3。123445显示不出来,而且是乱码,求高手帮忙指点,有什么好的办法么?请指教,谢谢!
#include <iostream>#include <unistd.h>#include <fcntl.h>#include <string>#include <stdio.h>#include <string.h>using namespace std; int main(int argc,char **argv) { FILE *fp1; int fd; long long fo = 3.123445; char b[8]; memcpy(b,&fo,sizeof(unsigned long)); string str1 = "hello word!";string str2 = "just do it!"; fp1 = fopen("test.txt","a+"); fd = fileno(fp1); write(fd,str1.c_str(),str1.length());write(fd,str2.c_str(),str2.length()); write(fd,b,8); }