写入一些文本到文件, 然后从文件读取回内存

写入一些文本到文件, 然后从文件读取回内存

[erphpdown]

; 在 AHK_L 42+, 使用 FileOpen 可以实现同样的目的.

FileSelectFile, FileName, S16,, Create a new file:
if FileName =
    return
GENERIC_WRITE = 0x40000000  ; 以写入而不是读取的方式打开文件.
CREATE_ALWAYS = 2  ; 创建新文件 (覆盖任何现有的文件).
hFile := DllCall("CreateFile", Str, FileName, UInt, GENERIC_WRITE, UInt, 0, Ptr, 0, UInt, CREATE_ALWAYS, UInt, 0, Ptr, 0, Ptr)
if not hFile
{
    MsgBox Can't open "%FileName%" for writing.
    return
}
TestString = This is a test string.`r`n  ; 通过这种方式写入内容到文件时, 要使用 `r`n 而不是 `n 来开始新行.
DllCall("WriteFile", Ptr, hFile, Str, TestString, UInt, StrLen(TestString), UIntP, BytesActuallyWritten, Ptr, 0)
DllCall("CloseHandle", Ptr, hFile)  ; Close the file.

; 现在已经把内容写入文件了, 重新把它们读取回内存中.
GENERIC_READ = 0x80000000  ; 以读取而不是写入的方式来打开文件.
OPEN_EXISTING = 3  ; 此标志表示要打开的文件必须已经存在.
FILE_SHARE_READ = 0x1 ; 这个和下一个标志表示其他进程是否可以打开我们已经打开的文件.
FILE_SHARE_WRITE = 0x2
hFile := DllCall("CreateFile", Str, FileName, UInt, GENERIC_READ, UInt, FILE_SHARE_READ|FILE_SHARE_WRITE, Ptr, 0, UInt, OPEN_EXISTING, UInt, 0, Ptr, 0)
if not hFile
{
    MsgBox Can't open "%FileName%" for reading.
    return
}
; 清空变量以便进行测试, 但需要确保它含有足够的容量:
BytesToRead := VarSetCapacity(TestString, StrLen(TestString))
DllCall("ReadFile", Ptr, hFile, Str, TestString, UInt, BytesToRead, UIntP, BytesActuallyRead, Ptr, 0)
DllCall("CloseHandle", Ptr, hFile)  ; 关闭文件.
MsgBox The following string was read from the file: %TestString%

[/erphpdown]

给TA捐赠
共{{data.count}}人
人已捐赠
其他

[dllcall]监视活动窗口并显示在它的焦点控件中垂直滚动条的位置

2017-12-31 19:11:16

其他

Win+C 时隐藏鼠标光标. 再次按下 Win+C 显示

2017-12-31 19:44:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索