C++配置lua并读写lua文件

描述

如何使用C++配置lua库,并读写lua文件

配置lua

  1. 进入lua官方网站http://www.lua.org
  2. 点击“Download”,下载最新版本的lua库
  3. 下载后的文件名类似于“lua-5.3.5.tar.gz”,解压它
  4. 解压后的文件夹应该包含一个“src”的文件夹,里面包含lua官方的api,包括很多.c和.h文件,还有一个makefile
  5. 将这个src文件夹拷贝到你的工程目录下,并且官网指示了如何编译这些文件,使用“make+操作系统”来编译这些api
    命令如下
    make macosx // mac系统
    make ubuntu // ubuntu系统
    
  6. 编译之后会生成很多.o文件,还会有一个名称为“liblua.a”的库文件
  7. 在你的工程CMakeLists下,链接库的路径中添加liblua.a的路径,例如:(OpenCV_LIBS是我需要的另一个库,只是写在了这里)
    target_link_libraries(test ${OpenCV_LIBS} /Users/admin/Desktop/try/lib/liblua.a)
    
  8. 你已经配置了lua,代码里以及可以调用lua的API了,下面一节会进行展示

读取lua文件中的内容

其实不太愿意写在这里,这里的代码功能,仅仅是从已知的lua文件去读取值,也能够去改变值。

但是改变后的值,并不能重新写到指定文件中去。那么改变值实际上也没有任何意义了,我完全可以从lua文件中读到想要的内容,对不想要的内容,我自己去定义一个变量来继续开发就好了啊,没必要再去改不对的值。

换句话说,以下的代码最重要的,还是能从lua文件中读取到数据,而不能更改lua文件内容

我自己其实有另外一种办法,去更改lua文件内容的,只不过有点先验设定和野路子,文章链接在下面

C++读写文件并更改已知内容中的参数
谁如果会更改lua文件内容,麻烦评论赐教,不胜感激

进入读取lua文件的正题吧

try.lua文件内容为

Table = {
    people = {
        name = "Neil",
        age = 18,
    },
}

代码为

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/plot.hpp>
#include <opencv2/imgproc.hpp>

#include <iostream>  
#include <string.h>  
using namespace std;  
   
extern "C"  
{  
    #include "lua/lua.h"  
    #include "lua/lauxlib.h"  
    #include "lua/lualib.h"  
}  

	
int main(int argc, char** argv)  
{  

    //1.创建Lua状态  
    lua_State *L = luaL_newstate();  
    if (L == NULL)  
    {  
        return 0;  
    }  
   
    //2.加载Lua文件  
    int bRet = luaL_loadfile(L,"/Users/admin/Desktop/CodeBase/c++_try/try.lua");  
    if(bRet)  
    {  
        cout<<"load file error"<<endl;  
        return 0;  
    }  
   
    //3.运行Lua文件  
    bRet = lua_pcall(L,0,0,0);  
    if(bRet)  
    {  
        cout<<"pcall error"<<endl;  
        return 0;  
    }  

    // 读取文件中的string格式
    lua_getglobal(L,"Table"); 
    lua_getfield(L,-1,"people");  
    lua_getfield(L,-1,"name"); 
    std::string str = lua_tostring(L,-1);  
    cout<<"people : name = "<<str.c_str()<<endl;

    // 更改字段的内容
    lua_pushstring(L, "Michael"); 
    lua_setfield(L, 2, "name");

    // 再次读取,会发现字段内容变为了"Michael"
    lua_getglobal(L,"Table"); 
    lua_getfield(L,-1,"people");  
    lua_getfield(L,-1,"name"); 
    str = lua_tostring(L,-1);  
    std::cout<<"people : name = "<<str.c_str()<<std::endl;

    // 读取文件中的number格式
    lua_getglobal(L,"Table"); 
    lua_getfield(L,-1,"people");  
    lua_getfield(L,-1,"age"); 
    if (lua_isnumber(L,-1)){
        string str1 = lua_tostring(L,-1);  
        cout<<"people : age = "<<str1.c_str()<<endl;  
    }
 
    // 更改文件中的number格式
    lua_getglobal(L,"Table"); 
    lua_getfield(L,-1,"people");  
    lua_getfield(L,-1,"name"); 
    lua_pushstring(L, "111");  
    string str2 = lua_tostring(L,-1);  
    cout<<"people : name = "<<str2.c_str()<<endl;  
    lua_pop(L, -1);

    // 关闭lua
    lua_close(L);  
    return 1;  
}

执行结果为

people : name = Neil
people : name = Michael
people : age = 18
people : name = 111
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值