Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
sshika

save in a file.txt

Question

7 answers to this question

Recommended Posts

  • 0

sorry i don't understand what you want.

if you want to save a table to a file, this is the code:

serial=require"serialization"
table={}
file=io.open("name","w")
file:write(serial.serialize(table))
file:close()
--or file=io.open("name","a") if you want to append the table instead of overwriting the file
Link to post
Share on other sites
  • 0

i wan't to realize an acquisition

 

example :

t(s);v1;v2;v3
0;1;0;1
1;0,999780683;0,06279052;1,062571203
2;0,99912283;0,125333234;1,124456064
3;0,998026728;0,187381315;1,185408043
4;0,996492859;0,248689887;1,245182746
5;0,994521895;0,309016994;1,30353889
6;0,992114701;0,368124553;1,360239254
7;0,989272333;0,425779292;1,415051625
8;0,985996037;0,481753674;1,467749711
9;0,982287251;0,535826795;1,518114046
10;0,978147601;0,587785252;1,565932853
11;0,973578903;0,63742399;1,611002893
12;0,968583161;0,684547106;1,653130267
13;0,963162567;0,728968627;1,692131194
14;0,957319498;0,770513243;1,72783274
15;0,951056516;0,809016994;1,760073511
16;0,94437637;0,844327926;1,788704296
17;0,937281989;0,87630668;1,81358867
18;0,929776486;0,904827052;1,834603538
19;0,921863152;0,929776486;1,851639637
20;0,913545458;0,951056516;1,864601974
21;0,904827052;0,968583161;1,873410214
22;0,89571176;0,982287251;1,877999011
23;0,886203579;0,992114701;1,878318281
24;0,87630668;0,998026728;1,874333408
25;0,866025404;1;1,866025404
26;0,85536426;0,998026728;1,853390989
27;0,844327926;0,992114701;1,836442627
28;0,832921241;0,982287251;1,815208491
29;0,821149209;0,968583161;1,78973237
30;0,809016994;0,951056516;1,760073511
31;0,796529918;0,929776486;1,726306404
32;0,783693457;0,904827052;1,68852051
33;0,770513243;0,87630668;1,646819923
34;0,756995056;0,844327926;1,601322981
35;0,743144825;0,809016994;1,55216182
36;0,728968627;0,770513243;1,49948187
37;0,71447268;0,728968627;1,443441307
38;0,699663341;0,684547106;1,384210446
39;0,684547106;0,63742399;1,321971096
40;0,669130606;0,587785252;1,256915859
41;0,653420604;0,535826795;1,189247399
42;0,63742399;0,481753674;1,119177664
43;0,62114778;0,425779292;1,046927072
44;0,604599115;0,368124553;0,972723668
45;0,587785252;0,309016994;0,896802247
46;0,570713568;0,248689887;0,819403455
47;0,553391549;0,187381315;0,740772864
48;0,535826795;0,125333234;0,661160029
49;0,518027009;0,06279052;0,580817529
50;0,5;1,22515E-16;0,5
51;0,481753674;-0,06279052;0,418963155
52;0,463296035;-0,125333234;0,337962802
53;0,444635179;-0,187381315;0,257253865
54;0,425779292;-0,248689887;0,177089404

i use a loop,

and in my loop i use  i = i + 1

 

i take some value and i use a string.format in a text variable, and i want to write my text variable on the line i

 

sorry, in english, it's difficult to explain.

Link to post
Share on other sites
  • 0

it is, but you have to think about a way to read it.

if your tables get saved like this:

 {table1};{table2};{table3};

you can read it by doing this:

local file=io.open("filename","r")
local text=file:read("*all")
file:close()
local tables={}
while true do
  local a=text:find(";")
  if a then 
   tables[#tables+1]=serialization.unserialize(text:sub(1,a-1))
    text=text:sub(a+1)
  else 
    break
  end end

Of course your tables shouldn't contain a ";" but you can change that to any symbol.

Link to post
Share on other sites
  • 0

This might be what you're looking for. I've added comments (like -- comment) in many places to explain what the code does, you don't need to copy them into your program.

-- Open the file statistics.txt in "w" (write) mode. This will overwrite existing files!
local f = open("statistics.txt", "w")

-- Run this loop 10 times and increase i each time
for i = 1, 10 do
  local a, b, c = -- This data needs to come from your reactor
  
  -- table.concat converts a table to a string, with a string (in this case ";") added between all elements
  local line = table.concat({i, a, b, c}, ";")
  
  -- Write line to f and add a newline at the end
  f:write(line .. "\n")
  
  -- Wait a second between measurements
  os.sleep(1)
end

-- Close the file
f:close()
Link to post
Share on other sites
  • 0

or the easy way without a new line, that will work with my previous code:

function save(table)
  local f=io.open("filename","a")
  f:write(serialization.serialize(table)..";")
  f:close()
end

that will result in adding an entry "{table};" to the file (without overwriting it)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.