to fake read() function

N

Noel Mosa

Hi,
i have a file in memory in a char* string(which i read using buffered
ifstreams from c++) and want to write a fake read() function for a C
program that usually reads from files.

What id like to know is the following:

how can i best copy the array to the buffer?

can i for example just say:




static int myindex;

int myRead(char* mysource ,char* buf, int size){

return strncpy(buf[myindex], mysource, size);
}

id'like myRead to behabe exactly as read(). inclusive return values and
beaviour at end of file. which lead us to my second important question:

how do i handle when the last sector is copied?
lets say i only have 3 bytes left (starting from the index) and the
program requests 10.
does strncpy terminates the string for me or do i have to take care of
generating an eof?

thanks for all answers!
also a pointer to a good read() reference is welcome!
 
I

Ian Malone

Noel said:
Hi,
i have a file in memory in a char* string(which i read using buffered
ifstreams from c++) and want to write a fake read() function for a C
program that usually reads from files.

read isn't standard C, but most of your question doesn't really
depend on this. I'm guessing you mean
ssize_t read(int d, void *buf, size_t nbytes) from Posix.
comp.unix.programmer may be a better place to ask.
What id like to know is the following:

how can i best copy the array to the buffer?

can i for example just say:
static int myindex;

I'll be pedantic and point out you don't initialise myindex
anywhere here.
int myRead(char* mysource ,char* buf, int size){

return strncpy(buf[myindex], mysource, size);
}

strncpy isn't appropriate here, since it copies C strings (\0
terminated). You want memcpy, and you'll have to tell it
how much to copy, which means you need to keep track of the
buffer size and current position. See
<http://lists.xiph.org/pipermail/vorbis-dev/2006-September/018536.html>
(I forgot to use memcpy in the above)

Secondly, compare your function declaration to the one I've
given above for read:
1. Should return ssize_t (I'm not certain this is a
standard C type, can anyone enlighten me?).
2. Takes int fd as its first argument. This isn't much use
to you, and suggests you might be better off emulating
fread instead. If you really have to emulate read
then the best you can hope to do is keep some table
for converting fd numbers to pointers for your buffers.
3. Types for arguments 2&3, there's no reason for these
to be different either.
id'like myRead to behabe exactly as read(). inclusive return values and
beaviour at end of file. which lead us to my second important question:

how do i handle when the last sector is copied?
lets say i only have 3 bytes left (starting from the index) and the
program requests 10.
does strncpy terminates the string for me or do i have to take care of
generating an eof?

As I mention above you can't expect strncpy to do this
for you as it's intended for handling real strings not
arbitrary data (there is a difference). Whether you
should generate EOF in this case? I'm not certain
whether you're allowed to.
thanks for all answers!
also a pointer to a good read() reference is welcome!

Since it sounds like you're programming for a Unix-like
system I'll suggest "man read". Otherwise the same
string in Google will get you what you need.
 
T

those who know me have no need of my name

in comp.lang.c i read:
Noel Mosa wrote:
I'll be pedantic and point out you don't initialise myindex
anywhere here.

the initialization is implicit, all objects with static duration are
initialized.
 

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
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top