473,473 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

to fake read() function

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!
Sep 11 '06 #1
2 2465
Noel Mosa wrote:
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.

--
imalone
Sep 11 '06 #2
in comp.lang.c i read:
>Noel Mosa wrote:
>static int myindex;
>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.

--
a signature
Oct 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fakhar | last post by:
Hi All, How can I check fake requests on my webpage. I am asking for email address as input and I wounder if anyone write a program to send fake requests and my system will be busy to respond...
1
by: Bryan Parkoff | last post by:
I work emulator project using 32 Bit emulated processor. The problem is that it attempts to allocate 4GB memory because some people want to use emulated instruction to read data from 4GB memory. ...
9
by: bubbakittee | last post by:
I am designing a package of functions to be used by people with very little programming experience (if any) and very little patience. I don't want to scare them with programmerish words like...
22
by: Ricky W. Hunt | last post by:
First, the subject probably doesn't use the correct terms but I'm not sure what it's called in VB. I'm writing a media player app. The subroutine that handles the "open file" button contains an...
5
by: mwebel | last post by:
Hi, is is also a C question but the program im writing is in C++ and it applies as well so: i have a program in the "main(int argc, const char ** argv)" and i want to make a library. for...
10
by: Konstantin Andreev | last post by:
Hello. Some time ago I asked in this conference, - How to use an ONLINE BACKUP to restore database onto another system? - but got no answers. Therefore I can conclude it is not possible. But......
3
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I don't know why, but I want to read a file, change some of the content, and want to write this new content in another file. The problem is, that it contains unicode text. My code is: ...
1
by: John Bode | last post by:
I need a way to fake reflection in C++ code that makes as few assumptions about the data types involved as possible. I suspect there is no good answer for what I need to do, but I'll present the...
4
by: Martijn Mulder | last post by:
You can fake the mechanism of inheritance by passing in a delegate function to the constructor. The constructor sets the delegate to a certain named function, Greet() in the example below. By...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.