• How to close a file if successfully opened

    From Mark Summerfield@mark@qtrac.eu to comp.lang.tcl on Thu Jun 20 10:16:56 2024
    From Newsgroup: comp.lang.tcl

    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
    }
    }
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Mark Summerfield@mark@qtrac.eu to comp.lang.tcl on Thu Jun 20 12:20:20 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Thu Jun 20 23:07:13 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Mark Summerfield@mark@qtrac.eu to comp.lang.tcl on Fri Jun 21 09:47:21 2024
    From Newsgroup: comp.lang.tcl

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114