Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing newline character

Status
Not open for further replies.

kingsleyuk2003

Programmer
Jul 9, 2008
1
NG
Please how can i print a newline character into a file, let say I have an information ABCDEF in a file 1. how can i make these letters appear in another file 2. in this format
A
B
C
D
E
F
thanks
 
The newline character is \n. Also, puts will insert a newline after each execution [red]unless[/red] the -nonewline option is specified.

So you have a couple of options depending on how the original data are organized. Let's say you read the entire dataset from the input as a single string, say strA:
Code:
set fid [open <input file name> r]
set strA [read $fid]
close $fid
Now you could parse strA and insert \n where you want it, and then
Code:
set fid [open <output file name> w]
puts -nonewline $fid $strA; #the newlines are already there
close $fid
Alternatively, you could parse the input on the read. Let's say each line will have a fixed number of bytes:
Code:
set fid [open <input file name> r]
while ![eof $fid] {
  lappend lstA [read $fid <number of bytes>]
}
close $fid
set fid [open <output file name> w]
foreach lineA $lstA {
  puts $fid $lineA
}
close $fid
Your data may be more complicated than this. If you tell us how the lines are to be configured, maybe we can help with that.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top