--- ./ini.tcl	2017-08-23 07:35:36.000000000 +0200
+++ ./ini_new.tcl	2017-08-17 08:14:36.000000000 +0200
@@ -57,21 +57,17 @@
 
     ::set fh ini$nexthandle
     ::set tmp [::open $ini $mode]
-    fconfigure $tmp -translation crlf
-    if {[info exists enc]} {
-	if {[catch {
-	    fconfigure $tmp -encoding $enc
-	} msg]} {
-	    ::close $tmp
-	    return -code error $msg
-	}
-    }
-
     namespace eval ::ini::$fh {
         variable data;     array set data     {}
         variable comments; array set comments {}
         variable sections; array set sections {}
     }
+    fconfigure $tmp -translation crlf
+    if {[info exists enc]} {
+	::ini::_setfileenc $tmp $enc
+	::set ::ini::${fh}::enc     $enc
+    }
+
     ::set ::ini::${fh}::channel $tmp
     ::set ::ini::${fh}::file    [_normalize $ini]
     ::set ::ini::${fh}::mode    $mode
@@ -105,6 +101,7 @@
     variable ::ini::${fh}::channel
     variable ::ini::${fh}::file
     variable ::ini::${fh}::mode
+    variable ::ini::${fh}::enc
     variable commentchar
 
     if { $mode == "r" } {
@@ -114,9 +111,13 @@
     }
     ::close $channel
     ::set channel [::open $file w]
+    if {[info exists enc]} {
+		::ini::_setfileenc $channel $enc
+    }
     ::set char $commentchar
     #seek $channel 0 start
     foreach sec [array names sections] {
+    variable ::ini::${fh}::enc
 	if { [info exists comments($sec)] } {
 	    puts $channel "$char [join $comments($sec) "\n$char "]\n"
 	}
@@ -126,12 +127,18 @@
 	    if {[info exists comments($sec\000$key)]} {
 		puts $channel "$char [join $comments($sec\000$key) "\n$char "]"
 	    }
+    if {[info exists enc]} {
+		::ini::_setfileenc $channel $enc
+    }
 	    puts $channel "$key=$data($sec\000$key)"
 	}
 	puts $channel ""
     }
     ::close $channel
     ::set channel [::open $file r+]
+    if {[info exists enc]} {
+		::ini::_setfileenc $channel $enc
+    }
     return
 }
 
@@ -141,6 +148,9 @@
 proc ::ini::_loadfile {fh} {
     variable ::ini::${fh}::data
     variable ::ini::${fh}::comments
+    if {[info exists enc]} {
+		::ini::_setfileenc $fh $enc
+    }
     variable ::ini::${fh}::sections
     variable ::ini::${fh}::channel
     variable ::ini::${fh}::file
@@ -214,6 +224,16 @@
     return
 }
 
+
+proc ::ini::_setfileenc {fh enc} {
+	if {[catch {
+	    fconfigure $fh -encoding $enc
+	} msg]} {
+	    ::close $fh
+	    return -code error $msg
+	}
+}
+
 # internal command to check validity of a handle
 
 if { [package vcompare [package provide Tcl] 8.4] < 0 } {
