Hello Guest

Writing text to a file and loading data from that file

  • 4 Replies
  • 593 Views
*

Keiichi4444

  • *
  • 1
  • +0/-0
    • View Profile
Writing text to a file and loading data from that file
« on: November 29, 2018, 01:55:19 am »
Hi all!

Can someone help me with function to use a separate file (i.e. dicelog.txt) to put some text there like i do it with "print" command and save it.
And if that's possible, can i somehow open that file take some info to var's in the working script?

i.e.

logfile = loadfile('dicelog.txt')
logfile:write(basebet = 0.00000010)
nextbet = logfile:read(basebet)
logfile:close()

Or smth like that) I just took this commands from my head, to show what i want to do.

Will appreciate ur help.

Ty.

*

Seuntjie

  • *****
  • 270
  • +2/-2
    • View Profile
Re: Writing text to a file and loading data from that file
« Reply #1 on: November 29, 2018, 08:10:52 am »
You can look into http://lua-users.org/wiki/IoLibraryTutorial

Why do you need to save variables to a text file though?

*

Blaksmith

  • **
  • 11
  • +1/-0
    • View Profile
Re: Writing text to a file and loading data from that file
« Reply #2 on: December 12, 2018, 01:46:33 am »
I use file reading/writing for doing logs of my wins, and here's a little excerpt from how I append to a file.

tempstr is the line of text to add to the file.

Code: [Select]
fin = assert(io.open(filename, "r"))
content = fin:read("*a")
fin:close()

fout = assert(io.open(tempfile, "w"))
fout:write(content)
fout:write(tempstr)

fout:close()
os.remove(filename)
os.rename(tempfile, filename)

Outline of what that code does is:
Read the whole file named in the variable filename.
Close that file
Open a new temporary file named in the variable tempfile
Write the contents of the previous file to the temporary file
Write the new output line(s)
Close the temporary file
Delete the original file
Rename the temporary file to the original filename

The reason that I have had to do it like this, rather than a straight append, is because for some reason, I think posted in a different thread, is that since the lua core that Seuntjie is using is old, and may not actually use the append feature.  I never could get it working.
« Last Edit: December 12, 2018, 01:52:14 am by Blaksmith »

*

Seuntjie

  • *****
  • 270
  • +2/-2
    • View Profile
Re: Writing text to a file and loading data from that file
« Reply #3 on: December 12, 2018, 08:52:42 am »
If you're only logging your wins, why not just query the dicebot.db file? It's a sqlite database that DiceBot logs every bet to.

If you only want to see your winning bets you can: Use the bet history form (view->bet history), select the timeframe you want to see your bets for
Use a program like sqlitebrowser to open and read the dicebot.db file. here you can query it as needed or export it to excel/csv etc

If you plan to use the information in your script, you can use the gethistory functions available in the programmer mode; see the functions tutorial in https://forum.seuntjie.com/index.php?topic=2.0

*

Blaksmith

  • **
  • 11
  • +1/-0
    • View Profile
Re: Writing text to a file and loading data from that file
« Reply #4 on: December 13, 2018, 04:27:47 am »
The reason I don't want to do it the way you are suggesting, is my output gives more info than just the database alone will in a single query.

Example of one of the outputs of a win.
Timestamp, rollID, rolls, betAmount, chance, spent, winProfit, runProfit, roll, high/low
2018-12-12 22:20:29, 3681034414, 532, 0.00000009, 0.41, 0.00001952, 0.00002166, 0.00000214, 99.6977, Betting High 

That last number before the actual roll itself, is the total amount of profit for the run since the last win.  Without querying all entries from win to win, it won't give me that info.  It's easier to just refresh a file than to write an sql to search for a range, then get the sum between the range, etc.