Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.c.moderated > #377
| From | rangsynth@gmail.com |
|---|---|
| Newsgroups | comp.lang.c.moderated |
| Subject | Re: portable code |
| Date | 2012-04-04 18:05 -0500 |
| Organization | http://groups.google.com |
| Message-ID | <clcm-20120404-0001@plethora.net> (permalink) |
| References | <clcm-20101216-0009@plethora.net> |
On Thursday, December 16, 2010 8:45:38 PM UTC+2, raffamaiden wrote: > Hi all. I'm writing a program wich will write some variables to an > output file. I do something like > > int a =5; > fwrite(&a, sizeof(int), 1, my_file_ptr); > > This will write an int to the file pointed by my_file_ptr. But i know > that the c standard does not specify the exact size in bytes for its > primitive type, as far as i know it only specifies that and int is an > integer type that rapresents a number with a sign, but different > implementations\operating systems can have different size for an int. > > So this mean that my program will write a 32 bit integer with one > implementation and a 16 bit with another implementation. This would > also mean that the file generated by the program that is running in > one implementation will not be readable in another implementation, > unless the program knows also in which implementation the instance > that generated the file was running. > That is right? I do not want such a behavior. How can i solve this? > -- > comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must > have an appropriate newsgroups line in your header for your mail to be seen, > or the newsgroup name in square brackets in the subject line. Sorry. You can say write((i >> 24) & 0xFF) write((i >> 16) & 0xFF) write((i >> 8) & 0xFF) write(i & 0xFF) This is because the integer of 32 bits you want has 4 bytes. so when you read it back simply... int out = 0; out += (read() >> 24); out += (read() >> 16); out += (read() >> 8); out += (read()); This will always work no matter the endian or binary type of your system, which means the code will be more portable. Also you will never have to worry about if the compiler will have int32_t and so on because you can just assume control of the 4 bytes. As a time saving measure do the work into a buffer of unsigned char first and then just write the whole buffer of the system. If you just have 2 bytes then same difference. Even will work with 8 bytes and __uint64 values. the example of 4 bytes is for unsigned int which is guranteed always containing 4 bytes worth of integer. -- comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.
Back to comp.lang.c.moderated | Previous | Next — Next in thread | Find similar
Re: portable code rangsynth@gmail.com - 2012-04-04 18:05 -0500
Re: portable code Dag-Erling Smørgrav <des@des.no> - 2012-04-23 08:31 -0500
Re: portable code Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-30 21:59 -0500
Re: portable code Dag-Erling Smørgrav <des@des.no> - 2012-05-04 17:29 -0500
Re: portable code Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-05-09 01:06 -0500
Re: portable code Jiří Zárevúcky <zarevucky.jiri@gmail.com> - 2012-05-04 17:30 -0500
csiph-web