作者Uihc (我 有我的倔強...)
站內Programming
標題[問題] (vc)用4bytes存浮點數並寫入二進位檔
時間Mon Nov 12 10:42:00 2007
請教各位前輩,現我想自己新增一個*.lan檔,這是一種影像格式
首先要先寫檔頭,它的檔頭固定是128byte
參考資料:
http://lab.geog.ntu.edu.tw/course/vb/Filehead.htm
現在我想寫入最後4個資料,MapX、MapY、CellX、CellY
但寫入的時候,每1個資料都要佔4個byte,於是,我寫了下面的程式碼
會出現 "
讀取位置 0xe9a90d38 時發生存取違規"
請問我應該怎麼做才能使浮點數寫入檔案時,是佔4個byte?? 謝謝
=============================程式碼片段================================
//開檔
ofstream outf;
outf.open("new.lan",ios::binary);
:(略)
char ch[4];
sprintf(ch,"%f",MapX);
//內容是194030.218750
outf.write(ch,sizeof(ch));
sprintf(ch,"%f",MapY);
//內容是2655145.250000
outf.write(ch,sizeof(ch));
sprintf(ch,"%f",lanheader.Cell_X)
//內容是8.000033
outf.write(ch,sizeof(ch));
sprintf(ch,"%f",lanheader.Cell_Y);
//內容是8.000033
outf.write(ch,sizeof(ch));
outf.close();
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.64.235
1F:推 FlyinDeath:都開binary了為什麼還用sprintf? 61.231.195.119 11/14 01:40