Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

The better part of valor is discretion. -- William Shakespeare, "Henry IV"


comp / comp.lang.tcl / Re: How to close a file if successfully opened

SubjectAuthor
* How to close a file if successfully openedMark Summerfield
+- Re: How to close a file if successfully openedMark Summerfield
`* Re: How to close a file if successfully openedgreg
 `- Re: How to close a file if successfully openedMark Summerfield

1
Subject: How to close a file if successfully opened
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 20 Jun 2024 10:16 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 Jun 2024 10:16:56 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: How to close a file if successfully opened
Newsgroups: comp.lang.tcl
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <bNicnXPg6cMFnOn7nZ2dnZfqnPadnZ2d@brightview.co.uk>
Date: Thu, 20 Jun 2024 10:16:56 +0000
Lines: 24
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-29vp3nm+QcRDwr8ww/rdxc9eHG1duisfy0GLmGMr+7qaGpDYNPHtBIu/Zof50TjzvSDNLiWszVfwfVX!d/L+tlTj56QN0kRaR+ceRNyfVzhXs6Gphy73pekJ/DgeslNHcJ3mTx5Uk7ZZqK0stsXti25CqwdZ!sUn/kmLE5eboeMkpBOUp46lk/Q==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I have an app that reads config info from a .ini file.
But the first time the app is run there is no .ini file exits.

Below is the code I'm using to ensure that the .ini file is closed if
opened; but surely there's a better way?

I really want to get rid of the first if command

if {![file isfile $::config::filename]} {
return
} try {
set fh [::ini::open $::config::filename -encoding utf-8 r]
set ::config::geometry \
[::ini::value $fh Window geometry [wm geometry .]]
tk scaling [::ini::value $fh Window scale [tk scaling]]
ttk::style theme use \
[::ini::value $fh Window theme [ttk::style theme use]]
set ::config::tab [::ini::value $fh Window tab]
} finally {
if {[info exists fh] && $fh ne ""} {
::ini::close $fh
}
}

Subject: Re: How to close a file if successfully opened
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Thu, 20 Jun 2024 12:20 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 Jun 2024 12:20:20 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: Re: How to close a file if successfully opened
Newsgroups: comp.lang.tcl
References: <bNicnXPg6cMFnOn7nZ2dnZfqnPadnZ2d@brightview.co.uk>
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <blSdnYwvfd4Zg-n7nZ2dnZfqn_ednZ2d@brightview.co.uk>
Date: Thu, 20 Jun 2024 12:20:20 +0000
Lines: 17
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-kYR9h+XVTRyNH0FDT+T+ZsT1J5jAe/9LvRiDc/Zgo0wwp9BcmAUAVqf4RPyleZvNaNGf6EURRXe6e2I!c9tfKLeV3La1yIQbnD4uVlWrsbAauEV6vK89lAyEyRn8VEQcEJyKImzDYgkVqooQnggDhpCrTvfA!z95k52NUgqOEPgiBMdvrTxP3dw==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I found a solution:

catch {
try {
set fh [::ini::open $::config::filename -encoding utf-8 r]
set ::config::geometry \
[::ini::value $fh Window geometry [wm geometry .]]
tk scaling [::ini::value $fh Window scale [tk scaling]]
ttk::style theme use \
[::ini::value $fh Window theme [ttk::style theme use]]
set ::config::tab [::ini::value $fh Window tab]
} finally {
if {[info exists fh] && $fh ne ""} {
::ini::close $fh
}
}
} ;# ignore if file not found

Subject: Re: How to close a file if successfully opened
From: greg
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Thu, 20 Jun 2024 21:07 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: gregor.ebbing@gmx.de (greg)
Newsgroups: comp.lang.tcl
Subject: Re: How to close a file if successfully opened
Date: Thu, 20 Jun 2024 23:07:13 +0200
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <v525m1$2ob03$1@dont-email.me>
References: <bNicnXPg6cMFnOn7nZ2dnZfqnPadnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 20 Jun 2024 23:07:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6054252a519acd5fef74fc7e4df6c80c";
logging-data="2894851"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OJlG1rFORKOLBjnORdMI60KR/rSOxGMY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GOAhPLc9/bJc99mWftO/peeTcZI=
Content-Language: de-DE
In-Reply-To: <bNicnXPg6cMFnOn7nZ2dnZfqnPadnZ2d@brightview.co.uk>
View all headers

Am 20.06.24 um 12:16 schrieb Mark Summerfield:
> I have an app that reads config info from a .ini file.
> But the first time the app is run there is no .ini file exits.
>
> Below is the code I'm using to ensure that the .ini file is closed if
> opened; but surely there's a better way?
>
> I really want to get rid of the first if command
>
> if {![file isfile $::config::filename]} {
> return
> }
> try {
> set fh [::ini::open $::config::filename -encoding utf-8 r]
> set ::config::geometry \
> [::ini::value $fh Window geometry [wm geometry .]]
> tk scaling [::ini::value $fh Window scale [tk scaling]]
> ttk::style theme use \
> [::ini::value $fh Window theme [ttk::style theme use]]
> set ::config::tab [::ini::value $fh Window tab]
> } finally {
> if {[info exists fh] && $fh ne ""} {
> ::ini::close $fh
> }
> }

Hello,
use trap with try
a example:

# tclWinError.c

proc writeFileWithHandling {filename data} {
try {
# r+ the file must already exist.
# generates an error if the file does not exist
set fd [open $filename r+]
puts $fd $data
puts "Data was successfully written to the file."
} trap {POSIX EISDIR} {} {
puts "Error POSIX EISDIR: It is a directory, not a file."
} trap {POSIX ENOENT} {} {
puts "Error POSIX ENOENT: The file does not exist."
} trap {} {} {
puts "Error {}: an unknown error occurred."
} finally {
if {[info exists fd]} {
close $fd
# Ensure the file is closed
}
}
}

# without puts
proc writeFile {filename data} {
try {
# r+ the file must already exist.
# generates an error if the file does not exist
set fd [open $filename r+]
puts $fd $data
puts "Data was successfully written to the file."
} trap {} {} {
} finally {
if {[info exists fd]} {
close $fd
# Ensure the file is closed
}
}
}

# example.txt exists in the directory
# example2.txt does not exist in the directory
# Example function calls
writeFileWithHandling "example.txt" "New data line"
writeFileWithHandling "example2.txt" "New data line"
puts "Output:"
puts $::errorInfo
puts \n

#without puts
writeFile "example.txt" "New data line"
writeFile "example2.txt" "New data line"

#Gregor

Subject: Re: How to close a file if successfully opened
From: Mark Summerfield
Newsgroups: comp.lang.tcl
Date: Fri, 21 Jun 2024 09:47 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!border-4.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 21 Jun 2024 09:47:21 +0000
From: mark@qtrac.eu (Mark Summerfield)
Subject: Re: How to close a file if successfully opened
Newsgroups: comp.lang.tcl
References: <bNicnXPg6cMFnOn7nZ2dnZfqnPadnZ2d@brightview.co.uk>
<v525m1$2ob03$1@dont-email.me>
MIME-Version: 1.0
User-Agent: Pan/0.154 (Izium; 517acf4)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <RJ-dna0mWOe00ej7nZ2dnZfqnPUAAAAA@brightview.co.uk>
Date: Fri, 21 Jun 2024 09:47:21 +0000
Lines: 23
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-RSwehpa11n06YhHyES/4nnmwqM1ftl3aZropKfGLjayhcZeljmuxyekJsZH7tFDa+6CGdJEKB3gNZ7b!2gzw39SfJ5PbqoV0v5LIEbGtK1bsDoJYFCnE5qKCkBvoZZx12SNLbsVUk6tb67b9nRvvCb+W443s!OzNgePTnw0eILQp6VkYKDAAaSA==
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
View all headers

I tried using both trap and finally but it didn't work.
Having seen your code quotes I realise that I made a mistake with the
syntax, so here's what I used in the end:

try {
set fh [::ini::open $::config::filename -encoding utf-8 r]
set ::config::geometry \
[::ini::value $fh Window geometry [wm geometry .]]
tk scaling [::ini::value $fh Window scale [tk scaling]]
ttk::style theme use \
[::ini::value $fh Window theme [ttk::style theme use]]
set ::config::tab [::ini::value $fh Window tab]
} trap {POSIX ENOENT} {} {
;# do nothing and rely on defaults
} finally {
if {[info exists fh] && $fh ne ""} {
::ini::close $fh
}
}

I'd gone wrong in the trap; I'd forgotten the second set of braces.

Thank you.

1

rocksolid light 0.9.8
clearnet tor