468,720 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Copying fread behaviour on char array

Hello everybody I want to create a routine that mimic the fread
behavior operating on char array.
I wrote

size_t fread_char(void* buf, size_t sz, size_t n, char* string_source)
{

memcpy(buf, string_source, sz*n);
string_source += n*sz;

return n;
}

When I leave the routine the string_source doesn't change its value, it
seems that the operation string_source += n*sz; has not been performed

Mar 31 '06 #1
3 2426

Giox wrote:
Hello everybody I want to create a routine that mimic the fread
behavior operating on char array.
I wrote

size_t fread_char(void* buf, size_t sz, size_t n, char* string_source)
{

memcpy(buf, string_source, sz*n);
string_source += n*sz;

return n;
}

When I leave the routine the string_source doesn't change its value, it
seems that the operation string_source += n*sz; has not been performed


Hint : Use

size_t fread_char(void* buf, size_t sz, size_t n, char**
string_source)

Mar 31 '06 #2
It works thans (I forgot stack :-))

Mar 31 '06 #3
Giox wrote:

Hello everybody I want to create a routine that mimic the fread
behavior operating on char array.
I wrote

size_t fread_char(void* buf, size_t sz, size_t n, char* string_source)
{

memcpy(buf, string_source, sz*n);
string_source += n*sz;

return n;
}

When I leave the routine the string_source doesn't change its value, it
seems that the operation string_source += n*sz; has not been performed


C passes parameters by value, so fread_char()'s "string_source" variable
contains the _value_ of the parameter passed to it. (Basically, in this
case, it's a copy of the pointer.)

When fread_char() modifies string_source, it modifies its own local copy,
not the caller's original variable.

One way to handle this is to pass the address of the char*, rather than
the char* itself:

size_t fread_char(void* buf, size_t sz, size_t n, char** string_source)
{
memcpy(buf, *string_source, sz*n);
*string_source += n*sz;
return n;
}

Another method would be to define a struct to correspond to the FILE
struct used by fread()/etc. and pass a pointer to the struct rather
than the buffer itself. (The struct would contain, at a minimum, the
same thing as "string_source" above. However, you might want to have
both an "original" string pointer and a "current" pointer, plus a
"length", should you want to be able to simulate fseek and the like.)
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Mar 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roy Hills | last post: by
2 posts views Thread by Josh Wilson | last post: by
9 posts views Thread by Franz Jeitler | last post: by
5 posts views Thread by David Mathog | last post: by
18 posts views Thread by mike3 | last post: by
1 post views Thread by kirany82 | last post: by
4 posts views Thread by Giacomo | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.