Thread: portable code
View Single Post
  #4 (permalink)  
Old 12-24-2010, 06:12 PM
Jasen Betts
Guest
 
Posts: n/a
Default Re: portable code

On 2010-12-16, raffamaiden <raffamaiden@gmail.com> 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?


read and write the file one byte at a time.

or use textual representations

fprintf(my_file_ptr,"%d ",a);

fscanf(read_file,"%d",&a);



--
⚂⚃ 100% natural
--
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.
Reply With Quote