Robert Latest said:
> On 24 Oct 2006 22:30:04 -0700,
> jobo <> wrote
> in Msg. < .com>
>
>> If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904"
>>
>> I want to capture each occurence of an integer 0-9 into an array.
>>
>> So in the array I would have {3,8,1,2,8,1,9,0,4}
>>
>> How would I go about doing this? scanf? fgets?
>
> Do two passes. First go through the file counting the digits,
> use the result to allocate a suitable array, and then go
> through the data again to populate the array.
Your technique has the advantage of preserving order, but the disadvantage
of requiring the stream to be read twice. (If it's stdin, which the OP
suggests it isn't but never mind that!, then the stream might not even be
available a second time.)
If you expand the buffer as you go, using realloc when required, then you
can do this in a single pass.
(If order is not significant, an array of ten unsigned longs will be fine,
with each element being a counter for a particular digit.)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)