473,224 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 2616

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
2
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0,...
9
by: Franz Jeitler | last post by:
Hello, I have some problems with fread.. first, let's see a part of the source file: FILE *fp; char buf; size_t nread; .. ..
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
18
by: mike3 | last post by:
Hi. I have an interesting problem. The C program presented below takes around 12 seconds to copy 128 MB of data on my machine. Yet I know the machine can go faster since a copying done at a...
1
by: kirany82 | last post by:
Hi all, I am writign a program to get the process no(linux) into an integer. i am not able to get the process no into integer array...this is the program. please help me. #include <stdio.h>...
4
by: Giacomo | last post by:
Hello.. i'm using php on linux --version: PHP 5.2.5 (cli) (built: Apr 25 2008 18:40:41) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies and...
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.