Connecting Tech Pros Worldwide Forums | Help | Site Map

treat array as a file

Jeevan
Guest
 
Posts: n/a
#1: Nov 13 '05
Hi,

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.

Thanks
Jeevan.

Irrwahn Grausewitz
Guest
 
Posts: n/a
#2: Nov 13 '05

re: treat array as a file


kjeevan_kumar@yahoo.com (Jeevan) wrote in
<a6a2b8c.0309040712.6d6ea829@posting.google.com> :
[color=blue]
>Hi,
>
>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[/color]
No way. But why should one want to do file operations on an array?
As you said, it resides in RAM, so you've got random access anyway ...
and maintaining a read/write index to a position in an array is no
big deal, is it? If you prefer, you could easily write functions
similar to those you mentioned, working on arrays instead of files.
Hint: read about sscanf(), strto*(), str*chr(), etc.
[color=blue]
>without having to write the data into a physical file and then read
>the file from the disk.[/color]

You can do this as well. It's like flying from Australia to Bali
via Norway. :)[color=blue]
>
>Thanks
>Jeevan.[/color]

Regards

Irrwahn.
--
Sig. Sic.
Kevin Bracey
Guest
 
Posts: n/a
#3: Nov 13 '05

re: treat array as a file


In message <a6a2b8c.0309040712.6d6ea829@posting.google.com>
kjeevan_kumar@yahoo.com (Jeevan) wrote:
[color=blue]
> 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.[/color]

This is fairly frequently asked - what you're looking for is some sort of

FILE *fopenmem(void *ptr, size_t len, const char *mode);

call. It would be nice, but unfortunately it doesn't exist. At least not
in the ISO C standard. Kind of a shame, as most implementations would
find it fairly straightforward to implement by setting the internal buffer
pointers to the user's object.

Actually, that's given me an idea. How about using setvbuf to set the buffer
size to the size of the file? Then the C library should just read the whole
thing into the buffer and won't need to touch the disk again.

--
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/
Eric Sosman
Guest
 
Posts: n/a
#4: Nov 13 '05

re: treat array as a file


Jeevan wrote:[color=blue]
>
> Hi,
>
> 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.[/color]

You can apply fopen() to anything your file system
will recognize as a named file, but to nothing else. If
your system supports file names like "RAM:08112200-08113fff"
or "/proc/1234/as" you're all set. Otherwise, you'll need
to find a different approach.

--
Eric.Sosman@sun.com
Tom Zych
Guest
 
Posts: n/a
#5: Nov 13 '05

re: treat array as a file


Kevin Bracey wrote:
[color=blue]
> This is fairly frequently asked - what you're looking for is some sort of
>
> FILE *fopenmem(void *ptr, size_t len, const char *mode);
>
> call. It would be nice, but unfortunately it doesn't exist. At least not
> in the ISO C standard. Kind of a shame, as most implementations would
> find it fairly straightforward to implement by setting the internal buffer
> pointers to the user's object.[/color]

That would be a handy function at times. Anyone know if anyone has
implemented it in a library?

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'gbzmlpu@cbobk.pbz' | rot13
Jeevan
Guest
 
Posts: n/a
#6: Nov 13 '05

re: treat array as a file


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.

Jeevan

Eric Sosman <Eric.Sosman@sun.com> wrote in message news:<3F5764EC.C90BB7C5@sun.com>...[color=blue]
> Jeevan wrote:[color=green]
> >
> > Hi,
> >
> > 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.[/color]
>
> You can apply fopen() to anything your file system
> will recognize as a named file, but to nothing else. If
> your system supports file names like "RAM:08112200-08113fff"
> or "/proc/1234/as" you're all set. Otherwise, you'll need
> to find a different approach.[/color]
LibraryUser
Guest
 
Posts: n/a
#7: Nov 13 '05

re: treat array as a file


*** rude and evil top-posting fixed ***

Jeevan wrote:[color=blue]
> Eric Sosman <Eric.Sosman@sun.com> wrote in message[color=green]
> > Jeevan wrote:[color=darkred]
> > >
> > > 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.[/color]
> >
> > You can apply fopen() to anything your file system
> > will recognize as a named file, but to nothing else. If
> > your system supports file names like "RAM:08112200-08113fff"
> > or "/proc/1234/as" you're all set. Otherwise, you'll need
> > to find a different approach.[/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]

Please do not top post.

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.

Any attempts to answer such questions here almost invariably end
up giving mis-information in some form or other. Thus the
queries should be addressed to newsgroups dealing with your
particular system(s).

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Kevin Bracey
Guest
 
Posts: n/a
#8: Nov 13 '05

re: treat array as a file


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/
LibraryUser
Guest
 
Posts: n/a
#9: Nov 13 '05

re: treat array as a file


*** rude and evil top-posting fixed ***

Jeevan wrote:[color=blue]
> Eric Sosman <Eric.Sosman@sun.com> wrote in message[color=green]
> > Jeevan wrote:[color=darkred]
> > >
> > > 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.[/color]
> >
> > You can apply fopen() to anything your file system
> > will recognize as a named file, but to nothing else. If
> > your system supports file names like "RAM:08112200-08113fff"
> > or "/proc/1234/as" you're all set. Otherwise, you'll need
> > to find a different approach.[/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]

Please do not top post.

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.

Any attempts to answer such questions here almost invariably end
up giving mis-information in some form or other. Thus the
queries should be addressed to newsgroups dealing with your
particular system(s).

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Kevin Bracey
Guest
 
Posts: n/a
#10: Nov 13 '05

re: treat array as a file


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/
Glen Herrmannsfeldt
Guest
 
Posts: n/a
#11: Nov 13 '05

re: treat array as a file



"Jeevan" <kjeevan_kumar@yahoo.com> wrote in message
news:a6a2b8c.0309040712.6d6ea829@posting.google.co m...[color=blue]
> Hi,
>
> 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.[/color]

There are some OS that supply this ability, but it is not standard. It
might be something like /dev/mem. One OS that I used to know that ran on a
machine with VME bus supplied device drivers for different VME address
spaces. (VME bus allows, for example separate decoding of a 24 bit address
space and a 32 bit address space. While they may overlap, that is not
required.) A device such as /dev/vme32d32 would allow 32 bit data cycles on
a 32 bit VME address space.

To use this feature, you would have to find where your array is in memory,
and fseek() to that place.

Again, completely non-portable.

There is also the non-portable mmap() that allows one to treat a disk file
as an array. The two features can even be combined!

-- glen


Dave Thompson
Guest
 
Posts: n/a
#12: Nov 13 '05

re: treat array as a file


On 4 Sep 2003 19:50:09 -0700, kjeevan_kumar@yahoo.com (Jeevan) wrote:

Please don't top-post. Context lost as a result.
[color=blue]
> 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). <snip>[/color]

<offtopic=system-specific> In Unix a socket descriptor *is* a file
descriptor. You can also use read() and write() instead of recv() and
send(); this can be considered either convenient or obfuscatory. Note
that dup'ing means that fclose'ing the FILE* leaves the (low-level)
socket open, *but* unless it's at EOF (and hence useless) stdio may
well have read ahead for buffering (probably making it useless).

In Windows socket handles are not file handles/identifiers, and can
only be used with winsock routines. I don't think there's any way to
do what you want in MS' runtime (MSVCRT.DLL etc.). Well, except
spawning threads which copy the socket input to a pipe whose other end
is used as a file and/or vice versa to output, or perhaps spawning a
proceess to do so if the socket can be opened there. Or somewhat less
unstandardly, copy socket input to a real but temporary file which is
used as input and/or direct output to such a file which you then copy
to socket output. </>

<offtopic=C++> If it's possible to get or change the code you want to
call to be C++ (which of course MS VC++ also supports) *and use C++
I/O instead of C I/O (FILEs)*, you can easily write (or find) a class
that wraps a socket into a streambuf. </>

- David.Thompson1 at worldnet.att.net
Closed Thread