[comp.lang.c] how to write

bobc@attctc.Dallas.TX.US (Bob Calbridge) (01/08/90)

A quick question if you please,  Suppose you want to write a binary constant
to a file.  Would the following code be valid?

	write(handle, (int *) '0x01', sizeof(int))

If so, is there any advantage to it in terms of program size, speed?
Or would it be better to assign the value to a variable and just point to it?
 
Thanks,
Bob
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=    More stupid questions available on request from                          =
-     bobc@attctc                     Your humble servant (real humble)       -
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

chris@mimsy.umd.edu (Chris Torek) (01/08/90)

In article <10883@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US
(Bob Calbridge) writes:
>A quick question if you please,  Suppose you want to write a binary constant
>to a file.  Would the following code be valid?
>
>	write(handle, (int *) '0x01', sizeof(int))

Not in C in general.  In C on a Unix machine, or a machine with Unix
compatible system calls, definitely not.  On a machine without a `write'
library routine / system call, the code would call an undefined function.

(Remember, this is `comp.lang.c', not `comp.lang.c.on.unix' nor
`comp.lang.c.on.ibm-pc' nor any other such thing.)

The best way to write constants to files is to write them in a printable
representation, unless there is some overriding reason (such as backwards
compatibility or---*after* testing, profiling, and tuning---insufficent
speed) to do otherwise.  The only `portable' way to write a binary value
is exemplified by the code

	int val = 1234;
	if (fwrite((void *)&val, sizeof(val), 1, stream)) != sizeof(val))
		... handle error ...

Unix and compatible systems may also provide `getw' and `putw'.  Note
that the order of bytes in the resulting file is machine dependent (and
perhaps even runtime system dependent).

>If so, is there any advantage to it in terms of program size, speed?

In most cases, there is a definite disadvantage in speed in avoiding stdio.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

cpcahil@virtech.uucp (Conor P. Cahill) (01/08/90)

In article <10883@attctc.Dallas.TX.US>, bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
> A quick question if you please,  Suppose you want to write a binary constant
> to a file.  Would the following code be valid?
> 
> 	write(handle, (int *) '0x01', sizeof(int))

No.  What this code would do is either core dump with a memory fault or write out
the sizeof(int) bytes starting at location 0x30783031 or location 0 depending 
upon how the compiler interpreted '0x01' but anyway, the value at this location
would probably not be a 1.

If you wanted to write out a 1, you would have to do something like:

	write(handle (char *) &0x01, sizeof(int))

But this is illegal since you cannot use & on a constant.  So what you have to
do is:

	intvar = 0x01;
	write(handle, (char *) &intvar, sizeof(int))




-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

reznick@ribs.UUCP (Larry Reznick) (01/10/90)

In article <21689@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
>
>	int val = 1234;
>	if (fwrite((void *)&val, sizeof(val), 1, stream)) != sizeof(val))
>		... handle error ...
>

The comparison is mistaken.  The fread() and fwrite() functions return
their 3rd parameters if there's no error.  So, the fragment should be
written as:

	if (fwrite((void *) &val, sizeof(val), 1, stream)) != 1) {
		... handle error ...
	}

----

-- 
Lawrence S. Reznick, KB6NSI	Systems Software Engineer
Alldata Corporation		Phone: (916) 684-5200  FAX: (916) 684-5225
9412 Big Horn Blvd, Suite 6	UUCP:  {sun!sacto,csusac}!ribs!reznick
Elk Grove, CA 95758		INTERNET: ribs!reznick@sacto.West.Sun.COM

chris@mimsy.umd.edu (Chris Torek) (01/10/90)

>In article <21689@mimsy.umd.edu> I wrote:
>>	if (fwrite((void *)&val, sizeof(val), 1, stream)) != sizeof(val))
>>		... handle error ...

In article <241@heel.ribs.UUCP> reznick@ribs.UUCP (Larry Reznick) writes:
>The comparison is mistaken.  The fread() and fwrite() functions return
>their 3rd parameters if there's no error.

Oops!  Quite right.  Sorry about that.

(See, you *can* find mistakes in my articles occasionally. :-) )
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris