Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!decwrl!ucbvax!huji.ac.il!amoss From: amoss@huji.ac.il (Amos Shapira) Newsgroups: comp.unix.programmer Subject: Re: File pointer to a memory location? Message-ID: Date: 12 Sep 90 13:16:12 GMT References: <119609@linus.mitre.org> Sender: daemon@ucbvax.BERKELEY.EDU Lines: 59 rtidd@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@mwunix.mitre.org FAST >#define DISCLAIM TRUE CHEAP > -pick any two Hope this helps, Amos Shapira amoss@batata.huji.ac.il