473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

treat array as a file

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.
Nov 13 '05 #1
11 3306
kj***********@yahoo.com (Jeevan) wrote in
<a6*************************@posting.google.com> :
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 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.
without having to write the data into a physical file and then read
the file from the disk.
You can do this as well. It's like flying from Australia to Bali
via Norway. :)
Thanks
Jeevan.


Regards

Irrwahn.
--
Sig. Sic.
Nov 13 '05 #2
In message <a6*************************@posting.google.com>
kj***********@yahoo.com (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.


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/
Nov 13 '05 #3
Jeevan wrote:

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.


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.

--
Er*********@sun.com
Nov 13 '05 #4
Kevin Bracey wrote:
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.


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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5
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 <Er*********@sun.com> wrote in message news:<3F***************@sun.com>...
Jeevan wrote:

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.


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.

Nov 13 '05 #6
*** rude and evil top-posting fixed ***

Jeevan wrote:
Eric Sosman <Er*********@sun.com> wrote in message
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.


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.


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.


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.
Nov 13 '05 #7
In message <3F***************@made.invalid>
LibraryUser <de**********@made.invalid> wrote:
Jeevan wrote:
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>
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.


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.


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.
Chuck Falconer, on vacation.


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/
Nov 13 '05 #8
*** rude and evil top-posting fixed ***

Jeevan wrote:
Eric Sosman <Er*********@sun.com> wrote in message
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.


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.


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.


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.
Nov 13 '05 #9
In message <3F***************@made.invalid>
LibraryUser <de**********@made.invalid> wrote:
Jeevan wrote:
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>
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.


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.


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.
Chuck Falconer, on vacation.


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/
Nov 13 '05 #10

"Jeevan" <kj***********@yahoo.com> wrote in message
news:a6*************************@posting.google.co m...
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.


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
Nov 13 '05 #11
On 4 Sep 2003 19:50:09 -0700, kj***********@yahoo.com (Jeevan) wrote:

Please don't top-post. Context lost as a result.
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>


<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
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
18
by: junky_fellow | last post by:
Consider an array. char arr; When we find sizeof(arr) ---> Output is 10, arr is treated as an object of 10 chars. When we say arr+1, ---> arr is treated as a pointer to char. Why is...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: Luis Esteban Valencia | last post by:
Hello. I have a method that receives a customer number, and it must return the customer credit which is in a file. like this. So if the parameter is 1, it must give me 450000. 1,450000...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
3
by: Jarry | last post by:
I have two arrays to load, taking one line to an entry from two 770,000 line files: so I have two arrays of 770000. But this can take upwards of 5 minutes, which is simply too long. I use a stream...
2
by: mostafijur | last post by:
Hello My PC has 2 ethernet card.I am using 1 port for internet.The 2nd port is connected with another PC and same network address.I want to send data by the 2nd port to that PC on time...
1
by: Thomas Robitaille | last post by:
Hello, I'm trying to write an array to a file, and I want to later read it in again. Some of the array elements are themselves arrays. I would also like to preserve the keys of the array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.