C++对Lua中table进行读取、修改和创建

C++代码:

// LuaAndC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


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


int _tmain(int argc, _TCHAR* argv[])
{
	 //1.创建一个state  
    lua_State *L = luaL_newstate();  

	luaL_openlibs(L);
	luaL_dofile(L,"Hello.lua");
	
	//获取table的值
	lua_getglobal(L,"str");

	//会将get的对应项压到栈顶
	lua_getfield(L,1,"name");
	lua_getfield(L,1,"age");

	if(lua_isstring(L,2))
	{
		cout<<"name= "<str={name="Hunter",age=18}


 

你可能感兴趣的:(Lua)