Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

In-place file record modification
Hi,

I would like to know if one can do in-place file editing in Tcl.
Example:
I have record such as
0078 100,99,10,11,11,0,0

to be replaced by
0078 100,99,10,11,11,7,9

Can one replace this record in this file without creating a new
file ?

Thanks in advance,
Anand


Report this thread to moderator Post Follow-up to this message
Old Post
Andy
12-27-04 08:58 PM


Re: In-place file record modification
Andy wrote:
>
> Hi,
>
> I would like to know if one can do in-place file editing in Tcl.
> Example:
> I have record such as
> 0078 100,99,10,11,11,0,0
>
> to be replaced by
> 0078 100,99,10,11,11,7,9
>
> Can one replace this record in this file without creating a new
> file ?
>
> Thanks in advance,
> Anand

Yes, you can. See the script below:


set outfile [open "updfile.txt" "w"]
puts $outfile "First write some text ...."
close $outfile

set inout [open "updfile.txt" "r+"]

puts $inout "xxxxx"

close $inout

Note:
- "r+" will do the trick
- puts will add an extra newline, unless you use the -nonewline option
- you can not _insert_ characters

Regards,

Arjen

Report this thread to moderator Post Follow-up to this message
Old Post
Arjen Markus
12-27-04 08:58 PM


Re: In-place file record modification
Andy <kaleanand@gmail.com> wrote:
: I would like to know if one can do in-place file editing in
Tcl.
: Example:
: I have record such as
: 0078 100,99,10,11,11,0,0
: to be replaced by
: 0078 100,99,10,11,11,7,9
: Can one replace this record in this file without creating a
new
: file ?

You can do it same way as text editors do: load entire file into memory,
modify there and save back. It is quite easy in Tcl

set f [open filename]
set content [read $f]
# insert code to modify content variable here
set f [open filename w]
puts -nonewline $content
close $f.

There is also possiblity to do random access to the file using s
command. But it is scarcely applicable to your case, because it would
quite hard work if line length changes during editing.

--

Report this thread to moderator Post Follow-up to this message
Old Post
Victor Wagner
12-27-04 08:58 PM


Re: In-place file record modification
Hi,

Thanks guys for the tips ..

I use the tclX package, I can do something like this

set line [bsearch $key $fp]
# Modify the record  returned by this
puts -nonewline $fp $line # Assuming the file is opened in overwrite
mode.

Any comments


Report this thread to moderator Post Follow-up to this message
Old Post
Andy
12-27-04 08:58 PM


Re: In-place file record modification
Andy schrieb:
> Hi,
>
> Thanks guys for the tips ..
>
> I use the tclX package, I can do something like this
>
> set line [bsearch $key $fp]
> # Modify the record  returned by this
> puts -nonewline $fp $line # Assuming the file is opened in overwrite
> mode.
>
> Any comments
>
The only way it is doable is for fixed size records. If you have those,
open the file with open, search the record to modify (or calculate its
position from your information about the format), use s to get there,
write your fixed sized record over the one in question and close the
file again. But if you need this kind of functionality it is usually a
better idea to use a real database like Metakit or SQLite for storing
the data.

Michael

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Schlenker
12-27-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:35 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.