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: cl...@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.
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
I can think of two approaches:
1. Use either short or long. They will be 16 or 32 bits respectively
on any(not sure, need confirmation ;-)) system.
2. Convert it to string and store. By doing this, the file will be
readable when opened as text file. But if you are keen on performance
then this is not good approach.
Regards,
Manohar
On Dec 16, 11:45 pm, raffamaiden <raffamai...@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?
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
Store your data in text form rather than binary. It takes more work to
parse, but it's far more portable and robust, and allows you to inspect
and even modify the result with a regular text editor.
DES
--
Dag-Erling Smørgrav - d...@des.no
Don't do that.
> 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.
Yes --- which is exactly why you shouldn't do that.
> That is right? I do not want such a behavior. How can i solve this?
By far the easiest solution is human-readable text output. In a
nutshell: use fprintf() instead of fwrite().
It can be done with fwrite(), but it takes quite a lot of extra work.
For starters, you won't be writing anything bigger than one byte
directly. You have to break it down to individual bytes yourself.
Otherwise you have no control over the relation between numerical values
and their representation in the file.
Yes. In addition to size, you have to worry about byte ordering and the
method used to represent signed integers (2's-complement is nearly
useful these days, but other representations are possible and have been
used).
> I do not want such a behavior. How can i solve this?
Start by deciding exactly what behavior you *do* want.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
The C Standard does not require this. Effectively short must be at least
16 bits (and there are systems where it is 32, and I think there was a
system where it was 24, two 12-bit words). Long must be at least 32-bits
and there were certainly systems where it was 64-bits.
> 2. Convert it to string and store. By doing this, the file will be
> readable when opened as text file. But if you are keen on performance
> then this is not good approach.
Not likely to be very significant. In general only use raw data in
temporary files (ones written and later read by the same process) There
are exceptions but fewer than the times when it is abused by people who
are fanatical about performance and then find that they have to spend
hundreds of hours porting code because the data files are not portable.
>
> Regards,
> Manohar
>
> On Dec 16, 11:45 pm, raffamaiden<raffamai...@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?
>> --
>> comp.lang.c.moderated - moderation address: c...@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.
--
Note that robinton.demon.co.uk addresses are no longer valid.
>Hi raffamaiden,
>
>I can think of two approaches:
>1. Use either short or long. They will be 16 or 32 bits respectively
>on any(not sure, need confirmation ;-)) system.
Not necessarily. A short must be at least 16 bits and a long must be
at least 32 bits but either or both can be more. And this does
nothing to solve the problem of reading the data on another system
with different a representation.
>2. Convert it to string and store. By doing this, the file will be
>readable when opened as text file. But if you are keen on performance
>then this is not good approach.
While obviously consuming more resources than simply reading or
writing the binary values, the cost of converting to text should be
relatively small compared to the overall cost of doing the I/O.
>On Dec 16, 11:45�pm, raffamaiden <raffamai...@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?
>> --
>> comp.lang.c.moderated - moderation address: c...@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.
--
Remove del for email
Jeremy
If you are reading and writing the int
No, it doesn't. It offers more basis types than C90 did, and some of
the new ones that C99 defined have specified size, or specified minimal
sizes. But that doesn't mean the existing basic types like int, long
ect. suddenly would have had their sizes specified.
But it doesn't require int32_t to exist. It's defined only if there's a
predefined type that meets its requirements (2's-complement, no padding
bits, exactly 32 bits).
On most modern systems it will exist -- and if you happen to encounter a
system where it doesn't, you'll find out about it very quickly when your
code fails to compile. (As opposed to code that just assumes int is
32 bits, which can fail quietly when that assumption is violated.)
If you don't actually need *exactly* 32 bits, you can use int_least32_t
or int_fast32_t, which are required to exist (they can be bigger than 32
bits).
Similar considerations apply to the uint*_t types, though of course
the 2's-complement requirement doesn't apply.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"