In message <3F5885A2.182C102A@made.invalid>
LibraryUser <deliberately@made.invalid> wrote:
[color=blue]
> Jeevan wrote:[color=green][color=darkred]
> > > Jeevan wrote:
> > > >
> > > > I have some data which I am getting from a socket. I am
> > > > currently storing the data in an array (so that future
> > > > reading of the data will be fast as it will be in RAM
> > > > instead of hard disk). Is there any way I can treat this
> > > > array as a file i.e. can I apply file operations (like
> > > > fopen, fscanf etc) on this array without having to write
> > > > the data into a physical file and then read the file from
> > > > the disk.
> > >
> > > <fancy filename suggestion from Eric Sosman>[/color]
> >
> > Thanks for the reply. Actually I am getting the data from a socket
> > which I am storing into an array. I found a round about method using
> > sockets and dup. The code is:
> >
> > int duplicate = dup(sock);//sock is the socket descriptor
> > FILE *fp = fdopen(duplicate);
> >
> > This worked in Unix but not in MS VC++ (dup is giving error). The
> > reason why I want to do file operations on data in array (or from
> > sockets) is that, there is a code which uses file operations (the data
> > to that code was in a file). Now I want to make the data coming from a
> > socket available to that code without making any changes to it.[/color]
>
> This problem is why your query is off-topic on c.l.c. Sockets,
> particular compilers and operating systems are not mentioned in
> the C standard, and thus such things are inherently non-portable.[/color]
For God's sake, his query wasn't in the slightest off-topic. His original
question didn't involve sockets, compilers or operating systems. What's wrong
with you people? Have you just programmed some sort of automated script
to say every single query is off-topic if certain keywords occur in a thread?
Is it a sin to explain WHY he wants to do something, and what the background
to the query is? If he didn't people would just ask why anyway.
His original query was "how can I treat an array as a file". Last time I
checked, C was advanced enough to include both of those two hip new computing
concepts, so it shouldn't be too much of a heinous crime to ask in
comp.lang.c whether they can be interworked.
The answer is, unfortunately, there is no portable way of doing this in ISO C
(so by merely asking whether there was, you are automatically off-topic, and
I'm probably off-topic by telling you there isn't).
This is odd, as most implementations of the stdio library probably do this
internally to implement sprintf(); it's natural and easy given the buffering
requirements of stdio. It would make sense to expose this internal working
portably via some sort of
FILE *fmemopen(void *ptr, size_t len, const char *mode);
sprintf() could then be implemented as
int sprintf(char *buf, const char *fmt, ...)
{
FILE *f = fmemopen(buf, SIZE_MAX, "w");
va_list ap = va_start(ap, fmt);
int result = vfprintf(f, fmt, ap);
va_end(ap);
fclose(f);
}
Maybe the original poster would like to propose this as an addition to
<stdio.h> in comp.std.c.
As ISO C doesn't (yet) provide what you're looking for, your next port of
call should probably be a POSIX newsgroup to see if you can find a
POSIX-standard method, maybe involving connecting to the s*cket directly, and
then beyond that platform-specific newsgroups.
[color=blue]
> Chuck Falconer, on vacation.[/color]
Some vacation, sitting in a library telling poor innocents asking sensible
questions that they're off-topic. I suggest you take a vacation from your
vacation. Seriously.
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW:
http://www.tematic.com/