473,441 Members | 1,853 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,441 software developers and data experts.

fmemopen: portable version?

In the GNU C library (stdio.h), there is a function
FILE* fmemopen (void *buf, size_t size, const char *opentype)

It opens a FILE stream that allows read or write access to the buffer
buf.

Does anyone know how to implement this so that it can also be used on
non-GNU systems? Only read access would be okay.

Thanks
Markus
Nov 14 '05 #1
5 16720
Markus Dehmann <ma*******@gmx.de> wrote:
In the GNU C library (stdio.h), there is a function
FILE* fmemopen (void *buf, size_t size, const char *opentype)

It opens a FILE stream that allows read or write access to the buffer
buf.

Does anyone know how to implement this so that it can also be used on
non-GNU systems? Only read access would be okay.


There's no way to do it in ISO C. If you're a glutton for punishment you
could override and provide custom implementations for fopen(), fread(),
fwrite(), fscanf(), et al, but you can't even manage that in a uniform way
across different Unices. And in any event that is _way_ beyond the scope of
anything standard C.

glibc has a peculiar (or not according to your persuasion) implementation
where fopen() maps a file into memory (using mmap()). Things like fscanf()
read from memory rather than doing lower-level platform I/O. fmemopen(), it
would seem, is a trap-door into this behavior.

Nov 14 '05 #2
Markus Dehmann wrote:
In the GNU C library (stdio.h), there is a function
FILE* fmemopen (void *buf, size_t size, const char *opentype)

It opens a FILE stream that allows read or write access to the buffer
buf.

Does anyone know how to implement this so that it can also be used on
non-GNU systems? Only read access would be okay.


There is no Standard C facility specifically designed
for this purpose. However, the Standard does not specify
the form or meaning of the file name string passed to plain
fopen(), so if the system attaches special meaning to file
names like "/proc/pid/as" you might be able to do what you
want. Highly non-portable, of course, and likely to be
tricky even on systems where it "works."

--
Er*********@sun.com

Nov 14 '05 #3

"Markus Dehmann" <ma*******@gmx.de> wrote in message

In the GNU C library (stdio.h), there is a function
FILE* fmemopen (void *buf, size_t size, const char *opentype)

It opens a FILE stream that allows read or write access to the buffer
buf.

Does anyone know how to implement this so that it can also be used on
non-GNU systems? Only read access would be okay.

Unfortunately there's no way to do this in ANSI C. I did suggest adding a
function to the standard library that would allow you to define your own
fputc / fgetc for a stream and then pass it to the stdio functions, but no
one took it up.
In C++ you can do this, in theory, by deriving a class from iostream.
Nov 14 '05 #4
In article <c1**************************@posting.google.com >,
Markus Dehmann <ma*******@gmx.de> wrote:
Does anyone know how to implement this so that it can also be used on
non-GNU systems? Only read access would be okay.


FILE *fmemopen (void *buf, size_t size, const char *opentype)
{
FILE *f;

assert(strcmp(opentype, "r") == 0);

f = tmpfile();
fwrite(buf, 1, size, f);
rewind(f);

return f;
}

On any particular implementation you may be able to work out how to
manually construct a FILE object that does what you want. For example,
on the system I'm using FILE is a struct which in part contains this:

/* operations */
void *_cookie; /* cookie passed to io functions */
int (*_close) __P((void *));
int (*_read) __P((void *, char *, int));
fpos_t (*_seek) __P((void *, fpos_t, int));
int (*_write) __P((void *, const char *, int));

I imagine that it would work if you set the cookie to the address of
your buffer and provided suitable functions for _read etc.

But of course if you want your code to be portable, you will have
to work this out for every system you care about.

-- Richard
Nov 14 '05 #5
On Tue, 13 Jul 2004 00:06:41 +0100, "Malcolm"
<ma*****@55bank.freeserve.co.uk> wrote:

"Markus Dehmann" <ma*******@gmx.de> wrote in message
[glibc fmemopen] opens a FILE stream [on memory]
Unfortunately there's no way to do this in ANSI C. I did suggest adding a
function to the standard library that would allow you to define your own
fputc / fgetc for a stream and then pass it to the stdio functions, but no
one took it up.
In C++ you can do this, in theory, by deriving a class from iostream.

<OT> For what is usually wanted when this question is asked,
the better answer in C++ is to implement your own child of streambuf,
which is then used by standard {i,o,io}stream. </>

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #6

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

Similar topics

14
by: John Smith | last post by:
Hi I'm looking for a header-file only STL implementation. I know the vendors provide one with compilers but often they are either buggy or has some other problems. My problem is that under...
14
by: Markus Dehmann | last post by:
The following if condition if((stream = fmemopen((void*)str, strlen(str), "r")) == NULL){ // ... } gives me a warning: assignment makes pointer from integer without a cast But only when I...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
4
by: Frederick Gotham | last post by:
I was pondering over writing a fully-portable version of <limits.h(e.g. such things as: #define UINT_MAX ((unsigned)-1) , when something occurred to me. Just recently on this newsgroup, I and...
3
by: yancheng.cheok | last post by:
i have a vendor function, which need to take into FILE* as function arguement. vendor_function_read_and_process(FILE* f) however, by reading from disk file during run-time, we get performance...
5
by: gordon.is.a.moron | last post by:
Hello, does anybody know of a good C++ compiler/IDE combo that would work from a USB key? I was using VS2003 and would like to use VC++ Express, but the chances of those working fof a USB key are...
3
by: ayan4u | last post by:
is there any standard portable version of _kbhit() ...as its requires non-portable conio.h....
37
by: Ioannis Vranos | last post by:
I think this is on topic here, because it is about C++ and available portable C++ GUI frameworks. I am looking for a decent portable C++ (meaning C++ API, not C) GUI framework that works both...
23
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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?

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.