Connect two pointers (arrays)

M

Mike Wahler

Pascal Ehlert said:
Hi!
As the subject says i want to connect two binarys arrays

Please specify what you mean by 'binary array'.
with content read out by fread.
Easy: I'm looking for a strcat function which works on binary..

Do you simply want to append data to an existing array?
Do you want to cause two separate arrays to become
adjacent in memory, or what?

-Mike
 
P

Pascal Ehlert

Hi!
As the subject says i want to connect two binarys arrays with content read out by fread.
Easy: I'm looking for a strcat function which works on binary..

Regards,
Pascal
 
K

Karthik Kumar

Pascal said:
Hi!
As the subject says i want to connect two binarys arrays

What is a binary array ?

with content read out by fread.
Easy: I'm looking for a strcat function which works on binary..

strcat is defined primarily for C-strings , whereas
fread can read any kind of data from files.
Also, if you are going to read a chunk of characters
using fread, it is not going to
insert the null character at the end for you.
Think about these aspects and post some compilable
code to get more help.
 
R

Richard Bos

Pascal Ehlert said:
As the subject says i want to connect two binarys arrays with content read out by fread.
Easy: I'm looking for a strcat function which works on binary..

There is no such thing, and there can be no such thing, for the simple
reason that it is not possible to deduce from the contents of binary
data where it ends. strcat() works by looking for the '\0' that
terminates the first string. This doesn't work for binary data, since
there is no such marker, and cannot be; after all, any byte that you use
for an end marker could just as well be valid binary data. In
particular, a byte with value 0 is often a normal part of binary data,
and cannot be used to terminate it; but neither can any other value.

To get around this problem, you'll simply have to remember how many
bytes you read, do a little pointer addition, and use memcpy().

Richard
 
P

Pascal Ehlert

Mike Wahler said:
Please specify what you mean by 'binary array'.
The data which was read out by fread.. I don't know how to call it.
Do you simply want to append data to an existing array?
Yes, that's what I want.. Like strcat does it. :)
Do you want to cause two separate arrays to become
adjacent in memory, or what?
Pascal
 
M

Mike Wahler

Pascal Ehlert said:
The data which was read out by fread.. I don't know how to call it.

OK, I think you're reading from a file which was opened in
'binary mode', i.e. 'fopen()'s second argument of "rb", and
passign 'fread()' an pointer to your array
Yes, that's what I want.. Like strcat does it. :)

Well you can't do it 'like strcat() does it' (unless you can
guarantee your file does not contain any characters with value
zero).

If you can make this guarantee:
Before you open the file, set the array element where you
want to start appending to zero. Then before each read,
search for that zero, pass its address to fread(), after
fread is done, set the array element after the read data
to zero. Keep doing this until you have all the data you
want, or end of file.


But all that isn't necessary. fread() will tell you (via
its return value) how many items were read (in the case
of characters, this is the same thing as bytes -- see
fread documentation for further details.) You can use this
information to specify the address (i.e. fread's first argument)
for subsequent calls to fread.

Finally remember to ensure that your array is large enough
to accomodate all the desired data.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top