File pointer to a memory location?

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Tue Sep 11 11:54:54 AEST 1990


In article <119609 at linus.mitre.org> rtidd at ccels3.mitre.org (Randy Tidd) writes:
: What I do now is query the database, get a block of memory, dump this
: memory to a temporary file, open the file with a file pointer, and
: pass the file pointer to the image processing routines.  Not only is
: this dumb, but images can be a big as 3 megs and this is incredibly
: inefficient.
: 
: Can anyone help me out?

It *definitely* counts as cheating, and it's not entirely portable, but
if you're desperate you can generally do something like this:

#include <stdio.h>

main()
{
    char *string = "Now is the time\nfor all good men\nto come to.\n";
    FILE *fake = fopen("/dev/null", "r");
    char buf[512];

    fake->_ptr = string;		/* pointer to your memory */
    fake->_cnt = strlen(string);	/* length of your memory */

    /* test it */

    while (fgets(buf,512,fake)) {
	fputs(buf,stdout);
    }
}

As soon as the _cnt runs down, it resets _ptr back to _base and refills
from the fd that's connected to /dev/null, so you get EOF.

If this doesn't work, look in /usr/include/stdio.h to see what the fields
are actually called.

Like I said, it's cheating.  But then again, many versions of sprintf()
do the same thing in reverse.

Larry



More information about the Comp.unix.programmer mailing list