将数组视为文件 [英] treat array as a file

查看:91
本文介绍了将数组视为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我从套接字获取了一些数据。我目前



数据存储在一个数组中(以便将来读取数据的速度很快

它将

在RAM而不是硬盘中。有什么方法可以把这个

数组作为一个文件处理,即我可以在这个数组上应用文件操作(如fopen,fscanf

等)

,无需将数据写入物理文件,然后从磁盘读取
文件。


谢谢

Jeevan。

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.

推荐答案

kj * ********* *@yahoo.com (Jeevan)写道:

< a6 **************** *********@posting.google.com> :
kj***********@yahoo.com (Jeevan) wrote in
<a6*************************@posting.google.com> :


我有一些数据,我从套接字获取。我目前将
数据存储在一个数组中(以便将来读取数据的速度很快,因为它将在RAM而不是硬盘中)。有什么方法可以将这个
数组作为一个文件处理,即我可以在这个数组上应用文件操作(如fopen,fscanf
等)
没办法。但是为什么要在阵列上进行文件操作呢?

如你所说,它驻留在RAM中,所以无论如何你都可以随机访问...

并且将读/写索引维护到数组中的位置是不是很重要,是吗?如果您愿意,您可以轻松编写类似于您提到的函数

,使用数组而不是文件。

提示:阅读sscanf(),strto *() ,str * chr()等

无需将数据写入物理文件,然后从磁盘读取文件。


您也可以这样做。这就像从澳大利亚飞往巴厘岛

通过挪威。 :)
谢谢
Jeevan。
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.




问候


Irrwahn。

-

Sig。 Sic。



Regards

Irrwahn.
--
Sig. Sic.


在消息< a6 ************************* @中发布。 google.com>
kj *********** @ yahoo.com (Jeevan)写道:
In message <a6*************************@posting.google.com>
kj***********@yahoo.com (Jeevan) wrote:
我有一些数据,我从套接字获取。我目前正在将数据存储在一个数组中(以便将来读取数据的速度很快,因为它将在RAM而不是硬盘中)。有什么方法可以把这个
数组作为一个文件处理,即我可以在这个数组上应用文件操作(如fopen,fscanf等)
而不必将数据写入物理文件并且
然后从磁盘读取文件。
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.




这是经常被问到的 - 你在寻找什么是某种


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


调用。这会很好,但遗憾的是它并不存在。至少不是ISO C标准中的
。有点遗憾,因为大多数实现都会通过设置内部缓冲区

指向用户对象的指针来实现它是相当简单的。


实际上,这给了我一个想法。如何使用setvbuf将缓冲区

大小设置为文件大小?那么C库应该只需要将整个

的东西读入缓冲区并且不需要再次触摸磁盘。


-

Kevin Bracey,首席软件工程师

Tematic Ltd电话:+44(0)1223 503464

182-190 Newmarket Road传真:+44(0) 1223 503458

剑桥,CB5 8HE,英国WWW: http:// www.tematic.com/


Jeevan写道:



我有一些数据,我从套接字中获取。我目前将
数据存储在一个数组中(以便将来读取数据的速度很快,因为它将在RAM而不是硬盘中)。有什么方法可以把这个数组作为一个文件对待,即我可以在这个数组上应用文件操作(如fopen,fscanf
等),而不必将数据写入物理文件然后从磁盘读取文件。

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.




您可以将fopen()应用于您的文件系统

将识别作为一个命名文件,但没有别的。如果

您的系统支持文件名,例如RAM:08112200-08113fff

或/ proc / 1234 / as;你已经准备好了。否则,你需要

才能找到不同的方法。


-
Er ********* @ sun.com


这篇关于将数组视为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆