I'm having simple issues again, I'm trying to read an int from a file, but I can't get it into a variable.
Reading the file into a string variable is no problem, but I can't convert from there to an int, on which I can do math.
The rosetta code example doesn't work.
I keep getting type mismatch.
How would I read a file into an int variable?
What is wrong with i.e. module parseutils?
http://nim-lang.org/parseutils.html
Have you ever tried something like parseIdent() or parseInt()?
Thanks Stefan
This doesn't look right, but it works:
var hitCounter: int
hitCounter = parseutils.parseInt(readFile("engine/hits.txt"), hitCounter)
You showed an interesting use case, hope the developers will comment on that.
But it is obvipusly not that what one would like to use. From the docs, parseInt() stores the int in its second argument and returns the number of processed digits if I remember correctly. Using the same variable for BOTH is strange. Of course you have always to watch out for conversion errors, if the string is not a valid int. Generally I would try something like
var digits_read = parseInt(readFile("engine/hits.txt"), hitCounter) # or discard parseInt(readFile("engine/hits.txt"), hitCounter)
I have never used module parseutils myself, so I can give you no detailed help. You may tell us how your file looks in detail, so maybe someone can give you a more detailed help. Maybe you need regex library for text file processing? I think Aporia text editor is reading configuration files, so maybe that can give us good examples.
Or use strutils' parseInt:
import strutils
for line in lines "hits.txt":
var x = parseInt line
echo x + 1
It's part of simple logging for my webserver, this particular file only contains one line, with numbers.
All I have to do is read it to use as a baseline when the server starts, increment on page loads, and save to file to keep log.
I used def's example, and ended up with:
hitCounter = parseInt readFile("engine/hits.txt")
Which seems to work just fine, I couldn't get strutils parseint to work earlier, I must have just been using it wrong.
Thanks guys, I love this forum :)