How to open a file in write mode in tcl




















Newcomers often try to write this with eof , and generally confuse themselves in the process [ L1 ]. It calls for. See also :: fileutil ::foreachLine , which does roughly the same thing. I just noticed that there isn't an example of writing a file, so here goes: note: When you are writing into a file , the contents of the file will not be visible till the end of the execution of the TCL script.

Its contents are visible only after the file has finished executing. LV Of course, if there is a need to see the file while it is open, just be certain your code invokes flush , which will force the output to the file instead of waiting until there is a large enough chunk of data to force out.

A simple tk example using the text widget is at Text Widget Example. But a file without newline at end of last line is not considered too well-behaved. Since in this example, a plain string is puts 'ed, I'd advocate a newline here. It's different when you save a text widget's content, where a newline after the last line is always guaranteed; there the -nonewline switch is well in place.

About the extra newline thing This is the proper behavior, even if it isn't technically required for most things these days. Unix text editors and tools still enforce it, however. Should it be considered a bug if Tcl behaves this way under Win32? LV This comment puzzles me. I have never seen a requirement for text files to end in blank lines. I have seen a few broken programs which generated an error if the last line in a file didn't end in a newline - but that doesn't create a blank line.

And I don't understand the last question - if Tcl behaves which way under Win32? In situations where output is line buffered default for text , puts -nonewline does not immediately deliver the output. Add a comment. Active Oldest Votes. Did you mean something like that? Improve this answer. But i have one more problem. If i want to write in a file so how can i substitute the data1,data You have to do e. The following code creates a text file in folder 'P' and writes into it.

Divya Bhaloidiya 4, 2 2 gold badges 23 23 silver badges 42 42 bronze badges. Tcl uses the open command to open files in Tcl. Opens an existing text file for reading purpose and the file must exist. This is the default mode used when no accessMode is specified. Opens a text file for writing, if it does not exist, then a new file is created else existing file is truncated. Opens a text file for writing in appending mode and file must exist. How can I change it so that it writes value of k and then new line and new value.

Here's a section of the man page for Tcl's open command:. Alternatively, you could rewrite your code so that the file is opened only once, outside the loop:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years, 1 month ago. Active 8 years, 1 month ago. Viewed 9k times. Will appreciate help.



0コメント

  • 1000 / 1000