lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi!

How do I deal with binary files in Lua?

I want to create a binary file using Lua code
to be used for a C++ program.

In lua:

file = fopen("file.dat", "wb")
fwritestring(file, "estutura1")
fwriteint(file, 12)
fwritefloat(file, 3.14)

fwritestring(file, "estutura2")
fwriteint(file, 33)
fwritefloat(file, 44.55)

fclose(file)

In C++

int n;
float f;
char str[STR_MAX];
fp = fopen("file.dat", "rb");

fread(str, STR_MAX, 1, fp);
fread(&, sizeof(int), 1, fp);
.
.
.

Is that possible?


Also, is possible to write to a file, in Lua, that a
C++ program could read as a struct?
something like this:

struct{

  char name[STR_MAX];
  int n;
  float f;
}struct_aux;


fread(&struct_aux, sizeof(struct_aux), 1, fp);

______________________________________________________________________

Yahoo! Mail: 6MB, anti-spam e antivírus gratuito! Crie sua conta agora:
http://mail.yahoo.com.br