Page 1 of 1

[DONE] read integer from file?

Posted: Sat Apr 03, 2010 8:22 pm
by PresFox
Hello,

I have a txt file, and in the first line it holds the last used ID for an item in a list view.

Now, when adding a new item, i want to read that old id, add 1, and use the new id for the list item, then update the old id in the txt file (so the next item gets a new id, etc.)

However, i cant use readstring, as i cant add 1 then, and readinteger returns 48 (as opposed to 0, whats in the file)

how do i do this?

Re: read integer from file?

Posted: Sat Apr 03, 2010 8:26 pm
by srod
x = Val(ReadString(...etc.))

?

Re: read integer from file?

Posted: Sat Apr 03, 2010 8:27 pm
by Demivec
PresFox wrote:I have a txt file, and in the first line it holds the last used ID for an item in a list view.

Now, when adding a new item, i want to read that old id, add 1, and use the new id for the list item, then update the old id in the txt file (so the next item gets a new id, etc.)

However, i cant use readstring, as i cant add 1 then, and readinteger returns 48 (as opposed to 0, whats in the file)
Because it's text use ReadString(). Because you want the number the string represents use Val().

@Edit: too slow.... :P

Re: read integer from file?

Posted: Sat Apr 03, 2010 8:44 pm
by Kaeru Gaman
btw.. it's not that cool to use a text file and text numbers for saving that.
better make it recordsets containing integers as a field of a structure.

since I don't know what you are planning, I just can recommend you take fixed recordsets into account...

Re: read integer from file?

Posted: Sat Apr 03, 2010 8:50 pm
by PresFox
that works, thanks!

now, i want to write the new id to the file, erasing the old one.

how can i erase the old one? (currently it writes the new id behindd it, so its 0123456 etc

Thanks in advance

Re: read integer from file?

Posted: Sat Apr 03, 2010 8:53 pm
by PresFox
after some more expirementing, i found more is wrong, the numbers dont match at all

this is my code:

Code: Select all

OpenFile(2,"settings.ini");
    oldid = Val(ReadString(2))
    newid = oldid +1
    Debug newid
    newid$ = Bin(newid)
    Debug newid$
    TruncateFile(2)
    WriteString(2,newid$)
it should read the old id from the file (wich is 0), turn it into a integer, add 1, turn it into a string again, and rewrite it back to the file, overwriting the old id.

needless to say, it isnt doing that, lol

Re: read integer from file?

Posted: Sat Apr 03, 2010 9:20 pm
by Arctic Fox
Try using Loc() and FileSeek()

Code: Select all

OpenFile(2, "settings.ini")
position = Loc(2)
oldid = Val(ReadString(2))

newid = oldid + 1
Debug newid

newid$ = Str(newid)
Debug newid$

FileSeek(2, position)
TruncateFile(2)

WriteString(2, newid$)
CloseFile(2)

Re: read integer from file?

Posted: Sat Apr 03, 2010 9:28 pm
by PresFox
that worked

thanks artic fox

Re: [DONE] read integer from file?

Posted: Sat Apr 03, 2010 10:39 pm
by srod
If you're working with some kind of ini file then consider using PB's preferences library which is ideal for this kind of thing.

Re: [DONE] read integer from file?

Posted: Sat Apr 03, 2010 11:02 pm
by Kaeru Gaman
...and if you plan to build up some kind of database, remember my tip of the fixed recordlength.