简体   繁体   中英

Cannot write files from Lua in Scite on Windows 10?

Using Scite 4.1.3 on Windows 10.

I tried the following Lua script:

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?! And also, this stupid Windows Explorer prompt shows up:

Windows资源管理器

At end, the content of the C:\Users\ME\AppData\Local\Temp\s8qk.m is still just 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 .

So my question is - how can I write a file with Lua in Scite on Windows? 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:

popen, pclose - pipe stream to or from a process

(emphasis mine).

So, basically, if under Windows I try to do 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).

However, Programming in Lua : 21.2 – The Complete I/O Model actually uses io.open (notice, no p for process); and then the files seem to open for read/write fine.

So, corrected example from OP should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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