[DONE] read integer from file?
[DONE] read integer from file?
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?
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?
Last edited by PresFox on Sat Apr 03, 2010 9:29 pm, edited 1 time in total.
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
Re: read integer from file?
x = Val(ReadString(...etc.))
?
?
I may look like a mule, but I'm not a complete ass.
Re: read integer from file?
Because it's text use ReadString(). Because you want the number the string represents use Val().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)
@Edit: too slow....

- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: read integer from file?
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...
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...
oh... and have a nice day.
Re: read integer from file?
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
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
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
Re: read integer from file?
after some more expirementing, i found more is wrong, the numbers dont match at all
this is my code:
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
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$)
needless to say, it isnt doing that, lol
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
Re: read integer from file?
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?
that worked
thanks artic fox
thanks artic fox
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
Re: [DONE] read integer from file?
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.
I may look like a mule, but I'm not a complete ass.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: [DONE] read integer from file?
...and if you plan to build up some kind of database, remember my tip of the fixed recordlength.
oh... and have a nice day.