luaL_loadfile
有时找不到file.lua
。我不累/path/to/file.lua
却不依赖于相对路径,但收到相同的错误:
"cannot open file.lua : No such file or directory"
我在luaL_loadfile
中使用了exec_lua_file
。
此问题通常在调用exec_lua
之后发生。我不知道该如何关联。
void exec_lua(char *command, uint8_t trim) {
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "Lua init error!\n");
goto end;
}
lua_pushinteger(L, a);
lua_setglobal(L, "a");
command = command + trim;
luaL_openlibs(L);
if(luaL_dostring(L, command)) {
fprintf(stderr, "Can't execute lua!\n");
goto end;
}
lua_getglobal(L, "a");
a = lua_tointeger(L, -1);
end:
lua_close(L);
return;
}
void exec_lua_file(char *filename, uint8_t trim) {
char path[1024];
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "Lua init error!\n");
goto end;
}
lua_pushinteger(L, a);
lua_setglobal(L, "a");
filename = filename + trim;
luaL_openlibs(L);
getcwd(path, 1024);
puts(path);
if(luaL_loadfile(L, filename) || lua_pcall(L, 0, 1, 0)) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
goto end;
}
lua_pop(L, lua_gettop(L));
lua_getglobal(L, "a");
a = lua_tointeger(L, -1);
end:
lua_close(L);
return;
}
已解决!问题是文件名末尾有空格。
此处可观察:"cannot open file.lua : No such file or directory"