[comp.unix.programmer] reading binary data in C

krasic@tucana.mpr.ca (Charles Krasic) (05/25/91)

I'm writing a program which needs to read a chunk of data
directly into memory and I'm having trouble deciding what is the
_best_ way to do it.  I've been considering three possibilities:

	1)  mmap the chunk of the file into memory with 
	    flags set to MAP_PRIVATE
	2)  malloc a chunk of memory and use either
	    i) fread
	    or ii) fgetc

Any of these should work (I think...) but I would like to know
which is best.  


    
--
Charles Krasic                  | Voice : (604) 294-1471
MPR Teltech Ltd. 		| Fax   : (604) 293-5787
8999 Nelson Way, Burnaby, BC    | E-Mail: krasic@mprgate.mpr.ca
Canada, V5A 4B5                 |         mprgate.mpr.ca!krasic@uunet.uu.net

icsu7039@ming.cs.montana.edu (Spannring) (05/31/91)

In article <KRASIC.91May24165634@tucana.mpr.ca> krasic@tucana.mpr.ca (Charles Krasic) writes:
[ lines about wanting to bring a chunk of a binary file into memory deleted ]
>
>	1)  mmap the chunk of the file into memory with 
>	    flags set to MAP_PRIVATE

I've never heard of mmap before and our man pages haven't either.  I
would guess it isn't portable, so I'd avoid it.

>	2)  malloc a chunk of memory and use either
>	    i) fread

Probaly not too bad, but if you are bringing in big (>=512 bytes)
chunks of data, why not go directly to the system calls and use read?
Should be about as fast as you can get.

>	    or ii) fgetc

I would guess somewhat slow.


--
====================================================================
 Six of one, 110 (base 2) of       | Craig Spannring
 another.                          | icsu7039@caesar.cs.montana.edu
 ----------------------------------+--------------------------------