如何使用Lua语言从磁盘加载Lua表格?

我在磁盘上有一份纯文本的 lua 表,看起来像这样:

rootTable = {
     name="myTable",
     settings = {
         setting1="some stuff",
         setting2="some more stuff"
     },
     parameters = {
          { name="paramName",  values={x=1.0,y=1.0,z=0.0,w=0.0} },
          { name="paramName2", values={x=0.0,y=3.0,z=1.0,w=0.0} }
     }
}

我试图在 lua (在 c++ 中) 中使用以下代码打开此表格:

const char *filename = "path/file.txt"

lua_State *state = luaL_newstate();
luaL_openlibs(state);
int error = luaL_loadfile(state, filename);
if (error == 0) {
    error = lua_pcall(_state, 0, 0, 0);
}
if(error != 0)
{
    std::string errorMessage = lua_tostring(_state, -1);
    fprintf(stderr, "%s\n", _errorMessage.c_str());
}

但无论何时我尝试运行它,我都会收到以下错误消息:

unexpected symbol near '{'

我不知道这是什么意思或我如何修复它。感谢任何帮助。

点赞
用户1442917
用户1442917

我在 Lua 5.1、5.2 或 5.3 中使用 load/ loadstring/ loadfile 加载那个表格时没有遇到任何错误 (也没有发现任何问题)。你可能加载的是不同于你展示的文件。

2016-01-22 19:13:52