找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4211|回复: 3

[游戏编程] [求助]如何用fwrite写二进制文件?

[复制链接]
发表于 2007-8-14 16:38:07 | 显示全部楼层 |阅读模式
目前只知道:
    ofstream bun("test.bun", ios::binary);
        char orz[] = "ZZZZZ";
        bun.write(orz, sizeof(orz));
        bun.close();
这样可以制作出一个.bun文件,但是打开来看,居然是看得见内容的-.-|||,按道理二进制文件应该是乱码才对吧……?还有就是怎么样记录除了char以外类型的资料呢?如果要用fwrite写又是怎么样的呢?
发表于 2007-8-14 17:42:38 | 显示全部楼层
其實用binary mode寫入字串是沒有意義的
Z的二進制碼便是ASCII Code代表Z的代碼,在wordpad開啟,wordpad把ASCII Code顯示成'Z'也是十分正常的事

某把先前作實驗用的源碼貼上來
實驗中使用了讀的程式
只要改成write便沒有問題
其中最重要的一項便是指標的轉換,重點所在
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main(){
  5.        ifstream fnt;
  6.         fnt.open("rockfont-8.fnt",ios::in|ios::binary);
  7.         char a0[6];
  8.         unsigned short maxwidth;
  9.         unsigned short height;
  10.         unsigned short ascent;
  11.         unsigned short pad;
  12.         unsigned int firstchar;
  13.         unsigned int defaultchar;
  14.         unsigned int size;
  15.         unsigned int nbit;
  16.         unsigned int noffset;
  17.         unsigned int nwidth;
  18.      fnt.read(a0,4);
  19.         fnt.read((char*)&maxwidth,2);
  20.         fnt.read((char*)&height,2);
  21.         fnt.read((char*)&ascent,2);
  22.         fnt.read((char*)&pad,2);
  23.         fnt.read((char*)&firstchar,4);
  24.         fnt.read((char*)&defaultchar,4);
  25.         fnt.read((char*)&size,4);
  26.         fnt.read((char*)&nbit,4);
  27.         fnt.read((char*)&noffset,4);
  28.         fnt.read((char*)&nwidth,4);
  29.         cout << "Max Width:" << maxwidth <<endl
  30.         << "Height:" << height <<endl
  31.         << "Ascent:" << ascent << endl
  32.         << "First Char:"<< firstchar << ":" << (char) firstchar<<endl
  33.         << "Default Char:"<< defaultchar << ":" << (char) defaultchar<<endl
  34.         << "Size:" << size << endl
  35.         << "Nbit:" << nbit << endl
  36.         << "Noffset:" << noffset << endl
  37.         << "Nwidth:" << nwidth << endl;
  38.         return 0;
  39. }
复制代码
发表于 2007-8-17 20:30:16 | 显示全部楼层
这么麻烦,偶觉得直接用fwrite写非常简单的……
2005稍微BT了点:
FILE* fp;
errno_t err;
err = _tfopen_s( &fp, TEXT(&quot;filename.bin&quot;), TEXT(&quot;wb&quot;) );
if ( err ) return -1; // failed
fwrite( buffer, sizeof(char), charlength, fp  );
fclose( fp );

以上……
发表于 2007-8-18 14:32:42 | 显示全部楼层
因為某習慣使用stream(給打飛)
  ofstream fnt;
        fnt.open(&quot;rockfont-8.fnt&quot;,ios:ut|ios::binary);
        unsigned int firstchar = 249;
        fnt.read((char*)&firstchar,sizeof(unsigned int));
        fnt.close
其實跟LS的不差很遠(內部當然差得遠)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|幻想森林

GMT+8, 2026-11-6 18:29 , Processed in 0.011560 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表