Re: [OT] A bit confused on pointers...
Re: [OT] A bit confused on pointers...
- Subject: Re: [OT] A bit confused on pointers...
- From: Andrew Farmer <email@hidden>
- Date: Wed, 28 Dec 2005 16:10:20 -0800
On 28 Dec 05, at 15:20, Sanri Parov wrote:
> I've got a file read with:
>
> fread (buffer, 1,size,pFile);
>
> in a for() cycle that reads several files. "buffer" is a char* and so
> another variable called finalBuffer (char*)
>
> All I want to do is to costantly update finalBuffer so it's the
sum of
> all the single buffer from every single file.
>
> finalBuffer +=*buffer
>
> is the right choice?
No. No no no no no.
*buffer isn't the size of the buffer, or the contents - it's the first
character of the buffer.
ASSUMING THAT:
1] buffer points to a memory block large enough to contain all the data
you're going to read at a time
2] finalBuffer is large enough to contain all the data you'll read,
overall
THEN, some reasonable code would be:
| char *bufferP = buffer;
|
| for(...)
| {
| size_t result = fread(bufferP, 1, size, pFile);
| if(result == 0)
| {
| handle the error and break out of the loop
| }
| bufferP += result;
| }
Remember that this will crash and burn if bufferP is too small to
contain all the data you're reading! Some more context might be
useful - what is this data being read from? Can you trust the source?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden