This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

edit file

Old posts that have not been replied to for several years.
Locked
r
ribot
Voice
Posts: 17
Joined: Mon Sep 08, 2003 4:01 pm
Location: eggbeer
Contact:

edit file

Post by ribot »

Hello :)

I have a file.txt with lists on each row such as:

Code: Select all

{pubm hello} {0 0}
{pubm hi} {0 0}
{pubm well} {pubm ok} {0 0}
What I want to do is to change these 0:s. I want to know how I can change the line:
{pubm hi} {0 0}
to
{pubm hi} {1 0}
and still keep all above and below lines intact.

I figured out that I will:
set var [gets {pubm hi} {0 0}]
then change the var to {pubm hi} {1 0}
but then to write it i don't know! because puts will either write at the end of the file or delete all data in the file. but i want to delete that one line and replace it with a new. however it's not that important to rewrite the new line on the same line number, as long as the script won't make extra line breaks.

this file.txt might become several MB big so I would also know if there are some restrictions like if I have to put all text after {pubm hi} {0 0} in a variable, then how much text does a variable fit?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Read the entire file in to memory, replace what you want then write it back to the file.
Once the game is over, the king and the pawn go back in the same box.
r
ribot
Voice
Posts: 17
Joined: Mon Sep 08, 2003 4:01 pm
Location: eggbeer
Contact:

Post by ribot »

how do i read it into memory?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

if you mean read file then write

Code: Select all

##### READ #####
set rfile [open "filname" "r"]
set data [read $rfile]
close $rfile
and the contents of the file should be $data

Code: Select all

##### WRITE #####
set wfile [open "filename" "w"]
puts $wfile "$something"
close
this will overwrite all, but migh also write all to only one line and keep overwriting it so .., read on it in tcl manual, there is append as wel and you can add + add stuff after the "w" "r" or "a"

its just an example eh ;) not sur that tis is what you wanted to do with it but ...
hope it gets you in the write direction he he

oh yes if you want to read it line by line you can also use while eof .. and gets to read the lines witch ever you prefer he he
XplaiN but think of me as stupid
Locked