tags:

views:

85

answers:

2

Basically I'm reading the contents of a file using fstream then converting it to const char* type. I'm supplying this to Lua, and Lua will do something with this. This however does not work. What does work is if I do:

const char* data = "print('Hello world')";
luaL_pushstring(L, data);
luaL_setglobal(L, "z");

They both are in the type const char* type and they are both the same string (e.g. I compared the two lengths). Except one works, and the other. I'm baffled. Any help here? Here is the code:

   std::string line,text;
   std::ifstream in("test.txt");
   while(std::getline(in, line))
   {
       text += line;
   }
   const char* data = text.c_str();
   luaL_pushstring(L, data);
   luaL_setglobal(L, "z");

Here is the Lua code:

loadstring(z)()
A: 

Don't you have to set the global before you push the value? Anyways, what's up, Camoy :P

Matt
+1  A: 

To diagnose this, you probably want to know more about what Lua thought. I'd write the Lua side as assert(loadstring(s))() instead. If loadstring fails, your current code at best prints an error from the attempt to call nil. With the assert() in the sequence, the call to nil will be replaced by a more informative error about what went wrong.

RBerteig