File pointer to a memory location?
Amos Shapira
amoss at huji.ac.il
Wed Sep 12 23:16:12 AEST 1990
rtidd at ccels3.mitre.org (Randy Tidd) writes:
>So what I have is a block of memory, having exactly the same size and
>contents of a file. What I have to do is pass my image processing
>routines a file pointer that points to this block of memory. If the
>routines used file *descriptors*, it wouldn't be a problem because I
>could just use pipes and be done with it.
There are two ways:
1. Once you have a file descriptor, you can get a file pointer to it with
fdopen(3s).
The bad thing about a pipes-based solution is that you are limited by the
size of the pipe, which is usually 4K.
2. The secnd solution, which seems to me much better, is to make what you
have suggest in your subject line, i.e. create a file pointer which points
to a memory location, this is how sscanf(3s) and co. works.
Here is a quick hack to demonstrate what I mean:
FILE *
memopen (cp, len)
char *cp;
int len;
{
FILE *fp;
if ((fp = (FILE *)malloc(sizeof(*fp))) == NULL)
return (NULL);
fp->_bufsiz = fp->_cnt = len;
fp->_base = fp->_ptr = (unsigned char *)cp;
fp->_flag = (short)_IOREAD _IOSTRG;
return (fp);
}
The interpretation of fields, as far as I remember, is:
_cnt: how much left to read from the buffer.
_bufsiz: the buffer size set by setbuf(3s) (in the above function, might be
better set to zero).
_base: the pointer to the first char in the buf (used to read a new buffer).
_ptr: pointer to next char to read.
Refferences: Definition of the getc(3s) macro in /usr/include/stdio.h
If you have access to sscanf(3s) then this is the ultimate answer.
Test by reading a real file pointer (e.g. stdin) and examinning
its fields between reads.
>Randy Tidd GOOD
>rtidd at mwunix.mitre.org FAST
>#define DISCLAIM TRUE CHEAP
> -pick any two
Hope this helps,
Amos Shapira
amoss at batata.huji.ac.il
More information about the Comp.unix.programmer
mailing list