|
From: Eric E. <eri...@sb...> - 2005-11-10 00:42:07
|
Scott, Try out this url: http://www.cprogramming.com/tutorial/cfileio.html Eric =20 -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Scott Sent: Wednesday, November 09, 2005 6:38 PM To: dev...@li... Subject: [Dev-C++] Ascii and Binary help =20 Okay this is a hard question to phrase.... I'm trying to do a simple command line utility in DevC, to write to a file..... =20 Now i have no problems writing=20 "Hello World" to the screen or text file. =20 But how do i write a binary file? for example... =20 The file i would like to store, looks like this when i cut and paste from notepad. =20 "FORM =88LWO2TAGS Red Void Void LAYR =12 PNTS BBOX VMPA =06VMAP WGHT Void.Weight = ?=80 POLS FACE PTAG COLR PTAG PART PTAG SURF [1]SURF = =A6Void COLR=20 ?H=C8=C9?H=C8=C9?H=C8=C9 LUMI =06 DIFF SPEC =06 TRAN = RIND =06 BUMP GLOS =06 VCOL ?=80 RGBAVoid.Vertex SMAN=20 _____ =20 =80 RFOP [1] TROP [1]" =20 =20 Now if i just cut and paste that and write it to a text file, like i do = with Hello World, why does it not work? Strangely enough, when i compare the 1byte decimal values of my written version, and the original version.. The first row of numbers are similar, but the 2nd row is different, and = then all the rest similar or identical..... for example... =20 =20 070 079 082 077 000 000 001 136 076 087 079 050 084 065 071 083 000 000 000 016 082 101 100 000 086 111 105 100 000 000 086 111 105 100 000 000 076 065 089 082 000 000 000 018 000 000 000 000 =20 070 079 082 077 032 032 001 136 076 087 079 050 084 065 071 083 032 032 032 016 082 101 100 032 086 111 105 100 032 032 086 111 105 100 032 032 076 065 089 082 032 032 032 018 032 032 032 032 =20 Okay now the script i wrote that originally does this.... -------------------------------------------------------------------------= --- -------------------------- generic { output =3D File("test.obj","wb"); output.writeData(test); output.close(); @data test=20 070 079 082 077 000 000 001 136 076 087 079 050 084 065 071 083 000 000 000 016 082 101 100 000 086 111 105 100 000 000 086 111 105 100 000 000 076 065 089 082 000 000 000 018 000 000 000 000 @end -------------------------------------------------------------------------= --- -------------------------------- =20 =20 The main difference, being that WB is used in the script,=20 which is obviously, Write Binary..... =20 Any ideas on how i do the C equivilant? here is the sample C code...=20 -------------------------------------------------------------------------= --- #include <stdio.h> #include <stdlib.h> void main() { FILE *myfile; myfile =3D fopen("test.obj","wb"); if(!myfile) { puts("Some kind of file error!"); exit(0); } fprintf(myfile,"FORM =88LWO2TAGS Red Void Void LAYR =12 PNTS BBOX VMPA =06VMAP = WGHT Void.Weight ?=80 POLS FACE PTAG COLR PTAG PART PTAG = SURF [1]SURF =A6Void COLR=20 ?H=C8=C9?H=C8=C9?H=C8=C9 LUMI =06 DIFF SPEC =06 TRAN = RIND =06 BUMP GLOS =06 VCOL ?=80 RGBAVoid.Vertex SMAN=20 _____ =20 =80 RFOP [1] TROP [1]"); fclose(myfile); } =20 So can i use Fprintf, to write my variable "test" which is an an = embedded 1byte decimal format? And get it written as binary? =20 Could it perhaps be something like printf("%03d ",bytes[i] &0xff);}} = (which is a rough guess) =20 Cheers, Scott. =20 |