读取和写入文件数组 [英] Read and write arrays to from file
问题描述
我有一个冒号分隔的字段里面写它在磁盘上创建会话文件的类。
<%
类MySession的
私人session_key可以,流 私人小组Class_Initialize
集流= CREATEOBJECT(的ADODB.Stream)
session_key可以= Request.Cookies时(sessionkey)
如果len(session_key可以)= 0,那么
了createSession()
万一
结束小组 从会话文件读
公共属性获取VAL()
在错误恢复下一个
VAL =
stream.open
stream.loadfromfileD:\\&放大器; session_key可以
VAL = stream.readtext
stream.close
高端物业 写会话文件
公共属性让VAL(userval)
在错误恢复下一个
stream.open
stream.writetext mycstr(userval)
为err11 = 0至50
stream.savetofileD:\\&放大器; session_key可以,2
如果Err.Number的= 0,那么对于退出
Err.Clear
下一个
stream.close
高端物业
末级
%GT;
现在填写会话文件我
设置的SessionID =新MySession的
FillArr = FillArr&安培; Request.Cookies时(sessionkey)及:&放大器;现在()+1
对于行= 0至UBOUND(arrRst,1)
FillArr = FillArr&安培; :&放大器; arrRst(行,0)
下一个
SessionID.val()= FillArr
要阅读会话文件的所有数据我用
ALLDATA = sessionID.val()
现在,我需要更多的字段添加到该文件。
问题是:
- 如何检查,如果这一领域的文件中已经存在?
- 如何在文件中只更新这个领域?
所以我觉得这是很好用,我可以通过它的名字叫阵列。
在我需要的数据会话文件中这样写:
{sessionkey:3yut267d273;session_exp:51453463,用户名:亚历克斯}
我想那一定是这样的,以获得所需的文件从申请:
ALLDATA = sessionID.val(用户名)
所以帮我写一个正确的语法要做到这一点:
- 如何写数组文件?
- 如何从文件中调用只需数组元素?
- 如何改变时才需要(或多个指定字段)的文件的数组元素,如果不存在,那么创建它?
的正规途径是要导入的文件(如果存在的话)到一个内存中的数据结构时,自定义对象被创建,并写入数据结构回到文件时,对象将被破坏。您的可以的每一个值请求或更改时间做到这一点,但你需要读取或每次写整个文件。
要你在问什么你需要一个抽象层映射的数据结构元素到磁盘字节。 NoSQL数据库是这样一个抽象层的实现。我从来没有一个工作,虽然,所以我不能告诉你如何建立从VBScript连接。从我从网上收集到的您可能需要使用REST API(通过 XMLHtt prequest
)。
I have a class which create session file on disk with written inside colon-separated fields.
<%
Class MySession
Private session_key, stream
Private Sub Class_Initialize
set stream = createobject("adodb.stream")
session_key = request.cookies("sessionkey")
if len(session_key) = 0 then
CreateSession()
end if
End Sub
' read from session file
Public Property Get val()
on error resume next
val = ""
stream.open
stream.loadfromfile "d:\" & session_key
val = stream.readtext
stream.close
End Property
' write session to file
Public Property Let val(userval)
on error resume next
stream.open
stream.writetext mycstr(userval)
for err11 = 0 to 50
stream.savetofile "d:\" & session_key, 2
if Err.Number = 0 then exit for
Err.Clear
next
stream.close
End Property
End Class
%>
Now to fill session file I do
Set SessionID = new MySession
FillArr = FillArr & Request.cookies("sessionkey") & ":" & now()+1
For Row = 0 to Ubound(arrRst,1)
FillArr = FillArr & ":" & arrRst(Row, 0)
Next
SessionID.val() = FillArr
To read all data in session file I use
AllData = sessionID.val()
Now I need to add additional field to this file. Problem is:
- How to check if this field already exist in the file?
- How to update only this field in the file?
So I think there is good to use arrays which I can call by it names.
In session file I need data to be written like:
{"sessionkey":"3yut267d273";"session_exp":"51453463";"username":"Alex"}
I think it must be something like this to get needed filed from file:
AllData = sessionID.val("username")
So help me write a right syntax to do that:
- How to write array to file?
- How to call only needed array element from file?
- How to change only needed (or multiple specified field) array element in file and if it not exist then create it?
The canonical way is to import the file (if it exists) into an in-memory data structure when the custom object is created, and write the data structure back to a file when the object is destroyed. You can do it every time a value is requested or changed, but you'll need to read or write the entire file every time.
To do what you're asking you'd need an abstraction layer mapping the data structure elements to bytes on disk. NoSQL databases are implementations of such an abstraction layer. I have never worked with one, though, so I can't tell you how to establish a connection from VBScript. From what I gleaned from the web you may have to use a REST API (via XMLHttpRequest
).
这篇关于读取和写入文件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!