Opening a FILE * on a buffer 
Author Message
 Opening a FILE * on a buffer

I'd like to open a FILE * on a memory buffer so that I can use f*() functions
for reading and writing to the memory area.  Do you know of a way to do that?
I've tried using setvbuf() but it doesn't behave predictably on different
OS's.
--
Open Source middleware available at http://www.*-*-*.com/ ~tgagne
--



Sat, 09 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer

Quote:

> I'd like to open a FILE * on a memory buffer so that I can use f*() functions
> for reading and writing to the memory area.  Do you know of a way to do that?
> I've tried using setvbuf() but it doesn't behave predictably on different
> OS's.

You don't do it that way.  Use a char[] (or char * to an allocated area
of memory) and use sprintf().

--

__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com
--



Sun, 10 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer


Quote:
>I'd like to open a FILE * on a memory buffer so that I can use f*() functions
>for reading and writing to the memory area.  Do you know of a way to do that?
>I've tried using setvbuf() but it doesn't behave predictably on different
>OS's.

That's because it's the wrong thing.

The short answer is, you can't really do this.  That's because the underlying
mechanisms for the f* functions are probably tied to the OS's idea of a
"file".

What's the expected benefit of being able to do this?  It would be the work
of 20 minutes or so to clone the most-used f* functions to interact with
a memory area; you'd just need
        mopen()
        mclose()
        mread()
        mwrite()
        mprintf()
        mgetc()
etc.

-s
--

C/Unix wizard, Pro-commerce radical, Spam fighter.  Boycott Spamazon!
Will work for interesting hardware.  http://www.plethora.net/~seebs/
Visit my new ISP <URL:http://www.plethora.net/> --- More Net, Less Spam!
--



Sun, 10 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer
Greetings,

Quote:



> >I'd like to open a FILE * on a memory buffer so that I can use f*() functions
> >for reading and writing to the memory area.  Do you know of a way to do that?
> >I've tried using setvbuf() but it doesn't behave predictably on different
> >OS's.

> That's because it's the wrong thing.

> The short answer is, you can't really do this.  That's because the underlying
> mechanisms for the f* functions are probably tied to the OS's idea of a
> "file".

> What's the expected benefit of being able to do this?  It would be the work
> of 20 minutes or so to clone the most-used f* functions to interact with
> a memory area; you'd just need
>         mopen()
>         mclose()
>         mread()
>         mwrite()
>         mprintf()
>         mgetc()
> etc.

True enough, but I'm guessing he wants to be able to ``drop-in'' a
pseudo-file (memory file) without having to change much code. Not that
terribly unrealistic really. The expected benefit is probably enhanced
functionality with minimal code changes.

The only way is probably to wrap all of the functions and pass your own
structure to them which is ugly as it will probably result in much extra
work.

--
Kyle A. York
Subordinate Grunt
CLOB
--



Mon, 11 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer
I have a server process which recieves a message from the client via a
socket.  The middleware yields a memory buffer which the server is then free
to parse.  I'd like to use fgets(), fgetw(), fgetc(), fputc(), to parse the
message in predictable ways.  I could write my own functions for emulating
f*()'s behavior but I was hoping to use a trick that had already been
discovered.  The more portable the better...

Anyway, thanks for everyone's suggestions!
--
Open Source middleware available at http://pweb.netcom.com/~tgagne
--



Sat, 16 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer


Quote:
> I have a server process which recieves a message from the client via a
> socket.  The middleware yields a memory buffer which the server is then
free
> to parse.  I'd like to use fgets(), fgetw(), fgetc(), fputc(), to parse
the
> message in predictable ways.  I could write my own functions for
emulating
> f*()'s behavior but I was hoping to use a trick that had already been
> discovered.  The more portable the better...

You can't use FILE * to open a memory buffer, and there's very little point
in doing it anyway. But you could use a char * in place of a FILE * to
cover most of it.

I'd do it by defining a new type, an instance of which carries around a
pointer to the data buffer, a size, and a "current byte" indicator, either
a size_t or a pointer. Then pass this thing around to your imitative
functions in the usual way.

example, using Buffer instead of File:

#include <stdio.h>

typedef struct BILE
{
  char *buffer;
  size_t size;
  size_t current;

Quote:
} BILE;

BILE *ropen(char *s, size_t len)
{
  BILE *b = malloc(sizeof *b);
  if(b != NULL)
  {
    b->buffer  = s;
    b->size    = len;
    b->current = 0;
  }
  return b;

Quote:
}

void bclose(BILE *b)
{
  free(b);

Quote:
}

Now bgetc, for example, is pretty trivial:

int bgetc(BILE *b)
{
  int i;
  if(size > current + 1)
  {
    i = buffer[current++];
  }
  else
  {
    i = EOF;
  }
  return i;

Quote:
}

bgets() and so on are really quite simple too (hint: look in K&R). But
really, we're just playing games with the language. Are you SURE you want
to do this?

--
Richard Heathfield

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

--



Sun, 17 Feb 2002 03:00:00 GMT  
 Opening a FILE * on a buffer
It doesn't seem to me to be playing games at all. What if you had an API
that picked up information from a file and you wanted to see what benefit
you could get out of putting the file's contents into shared memory and
having your N processes read it from memory than by doing file I-O? Is that
a game? Memory and files are just two forms of the same thing - information.
And it does seem a pity to have necessarily to handle them differently (at
least in a read-only mode!).
Has it never bothered you either than when you use Yacc and Lex straight out
of the packet you have to arrange things such that the stuff you're parsing
comes from STDIN?


Quote:

> But
> really, we're just playing games with the language. Are you SURE you want
to do this?


--



Sat, 23 Feb 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Opening a FILE * on a buffer

2. error opening 16th buffered file

3. can't open file: to many files open

4. Difference between MRU File Open and Regular File Open

5. Problems with send() on open socket, buffer overflow?

6. HOW TO Filter Files Without File Extention in Open File Dialog Box

7. File extension filtering for the file dialog presented for file opening

8. Problem opening a file for read while it is open for write

9. opening a file using open method

10. Problem with opening binary files using open()

11. CDaoDatabase::Open() will not open mdb file

12. How to active or open a particular file as soon as the projectwork space is open

 

 
Powered by phpBB® Forum Software