Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Loading a string and an integer from a text file?

Author
Message
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 19:44
I'm sure this was asked before but I couldn't find anything. How can I load a string from line 1 in a text file and an integer from line 2 from the same text file?
Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 20:20
Why not save everything as a string, just save the integers as strings and load the strings as integers. I will look for a snippet from my map editor.

B-e-a-utiful... Visit my site: www.cuso.tk
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 20:22
Here it is!
Save an integer called inta like this:

write string 1,str$(inta)

Load the integer again with this code:

read string 1,inta$
inta=val(inta$)

B-e-a-utiful... Visit my site: www.cuso.tk
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 20:35
But when i open the text file like this:



and i want to write in it like this:



I look in the text file and nothing has been added :S
How can I add something to the text file and how can I read from certain lines?

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 21:16
and how do i write to specified line?

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 16th Jul 2003 22:48
Does this work?

if file exist("test.txt")=1 then delete file "test.txt"
open to write 1,"test.txt"
write string 1,"DSADSADSADSADAS"
close file 1

B-e-a-utiful... Visit my site: www.cuso.tk
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 16th Jul 2003 23:22
RTSpider is correct - OPEN TO WRITE creates a brand new file which must not already exist, so you must delete the file first.

If the file already contains information you need, then simply read all the lines into a string array before deleting the file. Here's some example code:

Dim TextLines$(1000): Rem Can work with file of up to 1000 lines
If File Exist("test.txt")
LineCount=0
Open To Read 1,"test.txt"
Repeat
Inc LineCount
Read String 1,T$: TextLines$(LineCount)=T$
Until FILE END(1)=1
Close File 1
Delete File "test.txt"
Rem Make alterations to lines of text here by altering the contents
Rem of TextLines$(x) where 'x' is the line number.
LineCount=0
Open To Write 1,"test.txt"
Inc LineCount
Write String 1,TextLines$(LineCount)
Close File 1
Endif

Once all the existing lines are loaded, you can alter the array using the subscripts to determine the line number.

Finally, you delete the old file and write out all the new lines, along with the old ones which haven't changed.

TDK
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 17th Jul 2003 03:01
that code of TDK works great but there is 1 prob :S
when I put multiple lines in the text file and i run the program, only the first line is left in the text file :S
how can I solve this prob?

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 17th Jul 2003 03:30
This way it wil work:


B-e-a-utiful... Visit my site: www.cuso.tk
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 17th Jul 2003 16:25
GREAT JOB x-d , but there is still 1 prob
when i alter the content of a line as followed:



the text file doesn't changes :S
I don't know how to solve it so can someone help me?

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 17th Jul 2003 17:28
Well at my computer this works, here's the full code(and thanks TDK of course!)


B-e-a-utiful... Visit my site: www.cuso.tk
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 17th Jul 2003 19:27
GREAT JOB TDK AND RTSPIDER!!!!! x-d , works perfectly now

Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 10GB HD, Geforce 4 Ti4400 128mb ddr sdram
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 18th Jul 2003 00:10
Nice to see a problem solved!

B-e-a-utiful... Visit my site: www.cuso.tk
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 19th Jul 2003 00:47
Yes, sorry - I wrote that code straight into the forum post message box - it wasn't a paste from DB, so it wasn't tested. Apologies for the error.

At least it sounds like it helped point you in the right direction!

TDK
cusoi
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: Netherlands
Posted: 19th Jul 2003 19:53


B-e-a-utiful... Visit my site: www.cuso.tk

Login to post a reply

Server time is: 2024-09-20 15:55:50
Your offset time is: 2024-09-20 15:55:50