|
From: Reid T. <rei...@at...> - 2005-11-10 04:08:49
|
Eric Edwards wrote: > Scott, > > Try out this url: > > http://www.cprogramming.com/tutorial/cfileio.html > > Eric > > =20 > > -----Original Message----- > *From:* dev...@li...=20 > [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 > > "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 = =20 > BBOX VMPA =06VMAP WGHT= =20 > Void.Weight ?=80 POLS FACE PTAG COLR PTAG PART PTAG =20 > SURF [1]SURF =A6Void COLR > ?H=C8=C9?H=C8=C9?H=C8=C9 LUMI =06 DIFF SPEC =06 TRAN = RIND=20 > =06 BUMP GLOS =06 VCOL ?=80 RGBAVoid.Vertex SMAN > -----------------------------------------------------------------------= - > =80 RFOP [1] TROP [1]" > > =20 > > =20 > > Now if i just cut and paste that and write it to a text file, like i=20 > do with Hello World, why does it not work? > > Strangely enough, when i compare the 1byte decimal values of my=20 > written version, and the original version.. > > The first row of numbers are similar, but the 2nd row is different,=20 > 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 > > 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, > > which is obviously, Write Binary..... > > =20 > > Any ideas on how i do the C equivilant? > > here is the sample C code... > > -----------------------------------------------------------------------= ----- > > #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 =20 > =12 PNTS BBOX =20 > VMPA =06VMAP WGHT Void.Weight ?=80 POLS FACE PTAG =20 > COLR PTAG PART PTAG SURF [1]SURF =A6Void COLR > ?H=C8=C9?H=C8=C9?H=C8=C9 LUMI =06 DIFF SPEC =06 TRAN = RIND=20 > =06 BUMP GLOS =06 VCOL ?=80 RGBAVoid.Vertex SMAN > -----------------------------------------------------------------------= - > =80 RFOP [1] TROP [1]"); > fclose(myfile); > } > > =20 > > So can i use Fprintf, to write my variable "test" which is an an=20 > embedded 1byte decimal format? > > And get it written as binary? > > =20 > > Could it perhaps be something like printf("%03d ",bytes[i] &0xff);}}=20 > (which is a rough guess) > > =20 > > Cheers, > > Scott. > > =20 > http://www.die.net/doc/linux/man/man3/fwrite.3.html #include <stdio.h> #include <stdlib.h> int main() { FILE *myfile; char mydata[]=3D"FORM =88LWO2TAGS Red Void Void LAYR =20 =12 PNTS BBOX =20 VMPA =06VMAP WGHT Void.Weight ?=80 POLS FACE PTAG =20 COLR PTAG PART PTAG SURF [1]SURF =A6Void COLR\ ?H=C8=C9?H=C8=C9?H=C8=C9 LUMI =06 DIFF SPEC =06 TRAN = RIND =06 =20 BUMP GLOS =06 VCOL ?=80 RGBAVoid.Vertex SMAN =80 RFOP [1]= =20 TROP [1]"; myfile =3D fopen("test.obj","wb"); if(!myfile) { puts("Some kind of file error!"); exit(0); } fwrite(&mydata,sizeof(mydata),1,myfile); fclose(myfile); } |