[DONE] read integer from file?

Just starting out? Need help? Post your questions and find answers here.
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

[DONE] read integer from file?

Post 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?
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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: read integer from file?

Post by srod »

x = Val(ReadString(...etc.))

?
I may look like a mule, but I'm not a complete ass.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: read integer from file?

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: read integer from file?

Post 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...
oh... and have a nice day.
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

Re: read integer from file?

Post 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
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

Re: read integer from file?

Post 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
User of PB 4.41 - x86
On Windows 7
Intel Core I5 2.26GHZ
8 GB RAM
ATI Mobility Radeon HD 5650
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: read integer from file?

Post 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)
PresFox
User
User
Posts: 33
Joined: Sat Jan 19, 2008 9:30 am

Re: read integer from file?

Post by PresFox »

that worked

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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: [DONE] read integer from file?

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: [DONE] read integer from file?

Post by Kaeru Gaman »

...and if you plan to build up some kind of database, remember my tip of the fixed recordlength.
oh... and have a nice day.
Post Reply