Lua File Processing

Source: Internet
Author: User

I. File Read and storage:
-- Does not seem to support large files (cannot exceed 9 K ?)
Function filesaveload ()
Local file = Io. Open ("C: \ In. Lua", "R ");
Assert (File );
Local Data = file: Read ("* A"); -- read all content
File: Close ();
File = Io. Open ("C: \ out. Lua", "W ");
Assert (File );
File: Write (data );
File: Close ();
End
Filesaveload ();

Ii. non-circular table processing:

Hero = 1;
Monster = 2;
Building = 3;
Sumunit = 4;
Cha = {};
Cha [1] =
{
Basic =
{
Name = "farmer", -- NPC name
Cha_type = hero, -- NPC model
},
Combat =
{
Acquire = 600.00, -- range of active attacks
Basic_def = 10, -- basic defense
},
};

Function savetablecontent (file, OBJ)
Local sztype = type (OBJ );
Print (sztype );
If sztype = "Number" then
File: Write (OBJ );
Elseif sztype = "string" then
File: Write (string. Format ("% Q", OBJ ));
Elseif sztype = "table" then
-- Format the table content and write it to a file
File: Write ("{\ n ");
For I, V in pairs (OBJ) Do
File: Write ("[");
Savetablecontent (file, I );
File: Write ("] = \ n ");
Savetablecontent (file, V );
File: Write (", \ n ");
End
File: Write ("} \ n ");
Else
Error ("can't serialize a"... sztype );
End
End

Function savetable ()
Local file = Io. Open ("E: \ 00_00_00 _ overseas group _ learning \ write to .txt", "W ");
Assert (File );
File: Write ("Cha ={}\ N ");
File: Write ("Cha [1] = \ n ");
Savetablecontent (file, Cha [1]);
File: Write ("} \ n ");
File: Close ();
End

Savetable ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.