c调用lua 配置简单例子

本文介绍了一个使用Lua脚本语言与C++程序进行数据交互的示例。通过加载Lua脚本文件并读取其中的数据,展示了如何将Lua中的table类型转换为C++中的结构体数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "stdafx.h"
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};

typedef struct data
{
 string name;
 double rot;

}DATA,*LPDATA;

vector<DATA> vec;

void main( void)
{
 DATA info;
 lua_State* L = luaL_newstate();
 if ( luaL_dofile(L,"add1.lua"))
 {
  cout<<"Error to open file" <<endl ;
 }
 lua_getglobal(L,"data");
 if ( lua_istable(L, -1) != 1)
 {
  cout<< "data is not table" <<endl;
 }

 lua_pushnil(L);

 while ( lua_next( L ,-2 ) != 0 )
 {
  //printf("%s - %s/n",lua_typename(L, lua_type(L, -2)),lua_typename(L, lua_type(L, -1)));
  ///* 移除 'value' ;保留 'key' 做下一次迭代 */
  //lua_pop(L, 1);
  info.name = lua_tostring(L,-1);
  lua_pop(L,1);
  vec.push_back(info);
 }

 for ( int i = 0 ; i < vec.size() ; i++ )
 {
  cout<<vec[i].name  <<endl;
 }


 //cout<< temp[0].name <<"/t" <<temp[0].rot <<endl;
 //cout<<"--------------"<<endl;
 //cout<<temp[1].name <<"/t" <<temp[1].rot <<endl;

}

 

 

add1.lua

 

data =
{
 name = "jingpeng" ,
 roat = "kuku"
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值