K
To get a report of a reading error, you can use a function. lua_tostring and for your example, she should report:[string "io.write("Hello!..."]:1: unfinished string near '"Hello!'
This message says that Lua On her side, she found an unfinished line. The thing is, C, before turning the line anywhere, transforms the control symbol. \n and on the side Lua The code is produced as follows:io.write("Hello!
")
Which is syntaxically untrue.In order to avoid this mistake, we need to make sure C doesn't interfere or ruin the line. Double screens of the symbol manager: \\n♪ That's what's supposed to end up:#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) {
char script[] = "io.write("Hello!\n")";
lua_State L = luaL_newstate(); / открывает Lua /
luaL_openlibs(L); / открывает стандартные библиотеки */
int error = luaL_loadstring(L, script) || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1); /* снять сообщение об ошибке со стека */
}
lua_close(L); /* закрывает Lua */
return 0;
}
If you were uploading the violin from the file, there would be no such mistake, and there's no double-screen, of course, you don't have to.And last, if you had correctly dealt with the error, as in the example above, it would not be difficult to go ahead with the error.