I want to read the contents of a file. I m a begineer in TCK and
couldnt gather much information about file handling. If anyone of you
know please send me a sample script showing how to read from a file and
also please send me some documentation if its available with you
Thanks
Krishna
> I want to read the contents of a file. I m a begineer in TCK and
> couldnt gather much information about file handling. If anyone of you
> know please send me a sample script showing how to read from a file and
> also please send me some documentation if its available with you
I expect you mean Tcl (Tool Command Language)? That's what this group
is for- To read a file in one go is simple:
set fp [open $filename]
set data [read $fp]
close $fp]
To read it line by line (if it's gigabytes...)
set fp [open $filename]
while {[gets $fp line] >= 0} {
#do something with $line
}
close $fp
>
>krishna schrieb:
>
>> I want to read the contents of a file. I m a begineer in TCK and
>> couldnt gather much information about file handling. If anyone of you
>> know please send me a sample script showing how to read from a file and
>> also please send me some documentation if its available with you
>
>I expect you mean Tcl (Tool Command Language)? That's what this group
>is for- To read a file in one go is simple:
> set fp [open $filename]
> set data [read $fp]
> close $fp]
For the 'beginner in TCK': This line has a small typo and should read
close $fp
Welcome in Tcl land
Helmut Giese
I have a file with contents
------------------------------------
-file SYS -parameter NO 1 -parameter YO 2 -parameter CO 3
/netlist/name
i want to read the integers and also assign a variable
ie set a variable to /netlist/name
can someone give me how to handle these ?
Thanks,
krishna
Read the tutorial, either the section about regexp.
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl20.html
or use scan:
http://wiki.tcl.tk/scan
or use string functions to get at the parts the are interesting to you.
Michael
- you open the file with the 'open' command
- you read the file line by line with the 'gets' command
- you split the line into single words either with 'split' (may yield
empty fields if line contains consecutive spaces) or with 'regexp'
(check wiki and docs for syntax)
- you assign variables with the 'set' command
For all of those commands, the TCL pages
http://www.tcl.tk/man/tcl8.4/TclCmd/contents.htm
have the documentation online,
http://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html
describes the commands useful for reading files and the wiki
http://wiki.tcl.tk
may contain further info.
If you need help with specific code, please post that code.
HTH
R'
The contents of the file is
a ; b
c ; d
i Want to extract it as
a
c
in a variable
and
b
d
in another variable .
Is it possible?
I have another doubt if this is possible in TCL.
i have a file with data
-root RTI -parameter NUM 1 -parameter NO 3 -parameter TB 0
now i need to get the output as
RTI_NUM1_NO3_TB0
i would like to know how to set my search.
Thanks,
Krishna
set res ""
set spacer ""
foreach line $lines {
foreach tok $line {
switch -glob -- $tok \
-root {
puts $res
set res ""
# nothing
} -* {
set spacer _
} default {
append res $spacer$tok
set spacer ""
}
}
}
This gives one empty line at the top.
uwe
An interesting little problem. Try this:
set f [open ...]
# Extract the words from the file's contents
set data [regexp -all -inline {\S+} [read $f]]
close $f
# out is an accumulator variable
set out {}
# Iterate over the words; note that we do not use [foreach] here
# because we want to skip several words at a time, and the number of
# words to skip is not constant.
for {set i 0} {$i<[llength $data]} {incr i} {
switch -- [lindex $data $i] {
-root {
# Start of an item to print (?) so print out anything old
# that's accumulated first
if {[string length $out]} {puts $out}
# Now start accumulating the next thing to print
set out [lindex $data [incr i]]
}
-parameter {
# Add the data extracted from the -parameter options. Note
# that Tcl, unlike C/C++, has a well-defined substitution
# order so this code is correct.
append out _[lindex $data [incr i]][lindex $data [incr i]]
}
default {
puts "# WARNING: extra word [lindex $data $i] found!"
}
}
}
# Everything's processed, but we probably have something in the
# accumulator so print it out now so we don't lose the last line.
if {[string length $out]} {puts $out}
As far as I can see from testing with your example, that does the right
thing, but I hope that -root always takes one value and -parameter
always takes two; if not, you're going to have to take that code and
extend it further.
Donal.