gpt4 book ai didi

c - 在 Windows 上理解 Lua 的 os.tmpname()

转载 作者:行者123 更新时间:2023-12-04 13:51:00 25 4
gpt4 key购买 nike

我正在尝试使用 Lua 的 os.tmpname()在 Windows 上(在引擎盖下使用 tmpnam()),我对它返回的文件名有点困惑。

> print(os.tmpname())
\s3e8.
如果我像 io.open(os.tmpname(), "w") 一样直接将它提供给 Lua ,它将尝试在当前驱动器的根目录中创建文件。这似乎很不合适,因为我们通常没有这样做的权限。
但根据这个线程:
https://mingw-users.narkive.com/L7VR1gxX/temporary-file-woes
这显然应该是相对于当前目录的路径。他们从微软文档中提到了这个片段:

Note than when a file name is prepended with a back slash and no pathinformation, such as \fname21, this indicates that the name is validfor the current working directory.


但是我再也找不到他们在线程中提到的文档。
我用谷歌搜索,试图找到最新的文档并找到了这个:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tempnam-wtempnam-tmpnam-wtmpnam?view=msvc-160
这是上面文档中提到的生成规则:

_tempnam will generate a unique file name for a directory chosen by the following rules:

If the TMP environment variable is defined and set to a validdirectory name, unique file names will be generated for the directoryspecified by TMP.

If the TMP environment variable is not defined or if it is set to thename of a directory that does not exist, _tempnam will use the dirparameter as the path for which it will generate unique names.

If the TMP environment variable is not defined or if it is set to thename of a directory that does not exist, and if dir is either NULL orset to the name of a directory that does not exist, _tempnam will usethe current working directory to generate unique names. Currently, ifboth TMP and dir specify names of directories that do not exist, the_tempnam function call will fail.


我有 TMP定义了环境变量,但 Lua 总是生成我在顶部提到的那种路径。
真的,我有两个问题:
  • 这实际上应该是相对路径吗?
  • 如果是这样为什么它总是生成一个相对路径,而不是使用标准 TMP变量?

  • 我正在用 Lua 5.2 测试这个来自 LuaBinaries和 Lua 静态内置于 shinchiro的 mpv 构建。

    最佳答案

    根据 this thread在 lua-users.org 上,您可以尝试传递 null。

    well i solved it using tmpname(NULL,NULL)... it considers thetemporary directory of the system.when i use tmpnam() on windows on a non-writable root, the filenamegenerated (\something) is not writable...i guess it's an implementation bug


    另请注意 Lua 5.2 Reference Manual

    When possible, you may prefer to use io.tmpfile, which automatically removes the file when the program ends.

    关于c - 在 Windows 上理解 Lua 的 os.tmpname(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69426789/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com