簡體   English   中英

將浮動緩沖區寫入二進制文件

[英]write a float buffer to a binary file

  • 我有一個帶有ppm文件數據的浮動緩沖區。 緩沖區[0] [0]是第一個元素,緩沖區[3 * width * height] [0]是數據的最后一個元素。
  • 緩沖區具有這樣的元素。 1st = 117 2st = 135 3st =122。它是紅色,綠色和藍色。
  • 關鍵是將這些數據寫入二進制文件!

我嘗試這樣做,getHeight()返回數據的Height和getWidth()的寬度。

ofstream output(filename, ios::out | ios::binary);  
output.write((char *)buffer, img.getHeight() * img.getWidth() * 3);

我也嘗試這個,因為i = 0到i = 3 * height * width

fp = fopen(filename, "wb");
fwrite(reinterpret_cast<char*>(&buffer[i][0]), 1, 3*height*width, fp);

浮點數每個為4個字節。 fwrite()不知道您要編寫的類型,因此對於大小,還需要乘以每個元素的大小。

fwrite(reinterpret_cast<char*>(&buffer[i][0]), 1, 3*height*width * sizeof(float), fp);
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {
     ofstream out("blah.txt");
     float val = 10.0f;

     out << fixed << setprecision(5) << val << endl;
     out.close();
     return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM