简体   繁体   English

无法在 Windows 10 上的 Scite 中从 Lua 写入文件?

[英]Cannot write files from Lua in Scite on Windows 10?

Using Scite 4.1.3 on Windows 10.在 Windows 10 上使用 Scite 4.1.3。

I tried the following Lua script:我尝试了以下 Lua 脚本:

function TestFile()
  mycontent = "Hello World"

  mytmpfilename = os.tmpname()

  os.execute("echo aaaa > " .. mytmpfilename) -- create file explicitly;

  mytmpfile = io.popen(mytmpfilename, "w") -- w+ crashes Scite in Windows!
  mytmpfile:write(mycontent)
  mytmpfile:close()
  print("readall " .. mytmpfilename .. ": " .. io.popen(mytmpfilename, "r"):read("*a"))
end

If I run this, I get printed:如果我运行它,我会打印:

readall C:\Users\ME\AppData\Local\Temp\s8qk.m: 

... which means Lua could not even read this file?! ...这意味着 Lua 甚至无法读取此文件?! And also, this stupid Windows Explorer prompt shows up:而且,这个愚蠢的 Windows 资源管理器提示出现了:

Windows资源管理器

At end, the content of the C:\Users\ME\AppData\Local\Temp\s8qk.m is still just aaaa .最后, C:\Users\ME\AppData\Local\Temp\s8qk.m的内容仍然只是aaaa

So obviously, mytmpfile:write part fails silently, and nothing new is written in the file - the only thing that wrote to the file is the echo aaaa > ... executed by cmd.exe via os.execute .很明显, mytmpfile:write部分静默失败,文件中没有写入任何新内容 - 写入文件的唯一内容是cmd.exe通过os.execute执行的echo aaaa > ...

So my question is - how can I write a file with Lua in Scite on Windows?所以我的问题是 - 如何在 Windows 上的 Scite 中使用 Lua 编写文件? Preferably, without having that stupid "How do you want to open this file?"最好不要有那个愚蠢的“你想如何打开这个文件?” prompt show up?提示出现?

Eh, I think I got it ...呃,我想我明白了……

See, the OP example uses io.popen - and, if we look at https://man7.org/linux/man-pages/man3/popen.3.html it says:看,OP 示例使用io.popen - 如果我们查看https://man7.org/linux/man-pages/man3/popen.3.html ,它会说:

popen, pclose - pipe stream to or from a process popen, pclose - 进出进程的管道流

(emphasis mine). (强调我的)。

So, basically, if under Windows I try to do io. p open(filename)所以,基本上,如果在 Windows 下我尝试做io. p open(filename) io. p open(filename) , then apparently tries to find the process that would be the default "opener" for that file type ... and also therefore the prompt I'm being shown in the OP (and therefore, I could never read or write the files accessed -- or rather, not accessed -- in that way). io. p open(filename) ,然后显然试图找到该文件类型的默认“打开程序”的进程......以及因此我在 OP 中显示的提示(因此,我永远无法阅读或以这种方式写入访问过的文件——或者更确切地说,访问过的文件)。

However, Programming in Lua : 21.2 – The Complete I/O Model actually uses io.open (notice, no p for process);然而,在 Lua 中编程:21.2 – 完整的 I/O 模型实际上使用io.open (注意,进程没有p ); and then the files seem to open for read/write fine.然后文件似乎可以打开以进行读/写。

So, corrected example from OP should be:因此,来自 OP 的更正示例应该是:

function TestFile()
  mycontent = "Hello World"

  mytmpfilename = os.tmpname()

  -- os.execute("echo aaaa > " .. mytmpfilename) -- create file explicitly; -- no need anymore, with io.open

  mytmpfile = io.open(mytmpfilename, "w+") -- w+ crashes Scite in Windows, but only if using io.popen; is fine with io.open!
  mytmpfile:write(mycontent)
  mytmpfile:close()
  print("readall " .. mytmpfilename .. ": " .. io.open(mytmpfilename, "r"):read("*a"))
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM