FILE **pf = lua_newuserdata(L, sizeof *pf); *pf = 0; luaL_getmetatable(L, LUA_FILEHANDLE); lua_setmetatable(L, -2); *pf = /* your FILE* here */
Why is *pf initially set to NULL? -- http://rrt.sc3d.org/