Thread: [Dev-C++] A bit stumped on binary files
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ewald H. <in...@ho...> - 2004-05-21 20:26:59
|
Hi, I use the attached code to output a struct to a binary file. Outputting = character arrays is no problem, but how do I output an integer value in = bytes and not a string value ? I will be using the Win32 platform, = 32-bit integer, so portability is no issue. Is there an easy way to do = it that would simply output the required 4 bytes ? My current test = solution converts a number to a 10 digit string, padded with 0's like = this : 10 would be 0000000010, but it is clumsy! Regards Ewald Horn Proprietor/Eienaar : Horn Labs Cell/Sel : 083 519 1615 Website : http://www.hornlabs.co.za Email/E-pos : in...@ho... //[code] #include <fstream> #include <string.h> using namespace std; struct MyStruct { char name[40]; char surname[40]; int age; }; =20 // global ofstream myFile("tmp.out",ios::out | ios::binary); void writeStruct(MyStruct); int main() { MyStruct data[3] =3D {0}; =20 for (int i =3D 0; i < 3; i++) writeStruct(data[i]); =20 myFile.close(); =20 return 0; } =20 void writeStruct(MyStruct strc) { for (int i =3D 0; i < sizeof(strc.name); i++) myFile.put(strc.name[i]); for (int i =3D 0; i < sizeof(strc.surname); i++) myFile.put(strc.surname[i]); // problem area follows =20 myFile << strc.age; } =20 //[end code] |
|
From: Jorge A. <jor...@ep...> - 2004-05-22 03:10:12
|
In fopen you can specify a binary stream with the b modifier in the=20 wanted open type (glibc reference). Ewald Horn escribi=F3: > Hi, > =20 > I use the attached code to output a struct to a binary file.=20 > Outputting character arrays is no problem, but how do I output an=20 > integer value in bytes and not a string value ? I will be using the=20 > Win32 platform, 32-bit integer, so portability is no issue. Is there=20 > an easy way to do it that would simply output the required 4 bytes ?=20 > My current test solution converts a number to a 10 digit string,=20 > padded with 0's like this : 10 would be 0000000010, but it is clumsy! > =20 > Regards > Ewald Horn > Proprietor/Eienaar : Horn Labs > Cell/Sel : 083 519 1615 > Website : http://www.hornlabs.co.za > Email/E-pos : in...@ho... <mailto:in...@ho...> > =20 > //[code] > =20 > #include <fstream> > #include <string.h> > =20 > using namespace std; > =20 > struct MyStruct > { > char name[40]; > char surname[40]; > int age; > };=20 > =20 > // global > ofstream myFile("tmp.out",ios::out | ios::binary); > =20 > void writeStruct(MyStruct); > int main() > { > MyStruct data[3] =3D {0}; > =20 > for (int i =3D 0; i < 3; i++) > writeStruct(data[i]); > =20 > myFile.close(); > =20 > return 0; > }=20 > =20 > void writeStruct(MyStruct strc) > { > for (int i =3D 0; i < sizeof(strc.name); i++) > myFile.put(strc.name[i]); > =20 > for (int i =3D 0; i < sizeof(strc.surname); i++) > myFile.put(strc.surname[i]); > // problem area follows =20 > myFile << strc.age; > }=20 > //[end code] |
|
From: Ewald H. <in...@ho...> - 2004-05-22 21:03:44
|
>In fopen you can specify a binary stream with the b modifier in the >wanted open type (glibc reference). I do open the file as binary, using an IO stream. That's not the problem. I need to know how to convert an integer value into it's 4 byte equivalent. For instance 39184 is $00009910 and I would like to write [00,00,99,10] as hex values to my file. I was just wondering if there was a way to dump an entire struct to a file and then read it back later. Regards Ewald Horn Proprietor/Eienaar : Horn Labs Cell/Sel : 083 519 1615 Website : http://www.hornlabs.co.za Email/E-pos : in...@ho... |
|
From: Daniel K. O. <dan...@ya...> - 2004-05-22 21:23:34
|
Ewald Horn wrote: >I do open the file as binary, using an IO stream. That's not the problem. I >need to know how to convert an integer value into it's 4 byte equivalent. > > No conversion is needed. Actually, your problem is that you ARE converting it to a string. The << operator is overloaded to convert the integer in a string representation of the value. You should be using the .write() method of the stream. Check this: http://www.cplusplus.com/ref/iostream/ostream/write.html >For instance 39184 is $00009910 and I would like to write [00,00,99,10] as >hex values to my file. > > In that case you'll need the << operator with the "hex" manipulator. http://www.cplusplus.com/ref/iostream/ios_base/_dec_hex_oct.html Daniel K. O. |
|
From: Ewald H. <in...@ho...> - 2004-05-22 21:53:06
|
> No conversion is needed. Actually, your problem is that you ARE > converting it to a string. The << operator is overloaded to convert the > integer in a string representation of the value. You should be using the > .write() method of the stream. > > Check this: > http://www.cplusplus.com/ref/iostream/ostream/write.html Awesome! Funny my C++ manual does not mention this write method. Guess it's time for a new one then. Thank you for all the answers. Thank you Ewald Horn Proprietor/Eienaar : Horn Labs Cell/Sel : 083 519 1615 Website : http://www.hornlabs.co.za Email/E-pos : in...@ho... |
|
From: Per W. <pw...@ia...> - 2004-05-22 21:26:43
|
Just do: int i = 10; (char*)i; and write out sizeof(i) bytes. /Per W On Sat, 22 May 2004, Ewald Horn wrote: > >In fopen you can specify a binary stream with the b modifier in the > >wanted open type (glibc reference). > > > I do open the file as binary, using an IO stream. That's not the problem. I > need to know how to convert an integer value into it's 4 byte equivalent. > For instance 39184 is $00009910 and I would like to write [00,00,99,10] as > hex values to my file. I was just wondering if there was a way to dump an > entire struct to a file and then read it back later. > > > Regards > Ewald Horn > Proprietor/Eienaar : Horn Labs > Cell/Sel : 083 519 1615 > Website : http://www.hornlabs.co.za > Email/E-pos : in...@ho... > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: Oracle 10g > Get certified on the hottest thing ever to hit the market... Oracle 10g. > Take an Oracle 10g class now, and we'll give you the exam FREE. > http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |