开发者

Problem with skipping lines

The following code is skipping odd lines like 1,3,5,7,9......wat has to be开发者_运维百科 done to get all lines from a file using this code

set in [filename r]
seek $in 0 start
while { [gets $in line] != -1 } {
    gets $in line
    puts $line
}


You're doing gets $in line once in the condition and once inside the loop body; the line read in the condition gets lost as a result. You probably want to remove the one in the loop body.


You have used gets twice that is why you are getting only the odd lines

Other solution:

Instead of using gets I prefer using read function to read the whole contents of the file and then process those line by line. So we are in complete control of operation on file by having it as list of lines

set fileName [lindex $argv 0]

catch {set fptr [open $fileName r]} ;

set contents [read $fptr] ;#Read the file contents

close $fptr ;Close the file since it has been read now

set splitCont [split $contents "\n"] ;#Split the files contents on new line

splitCont is the list which has has lines of the file as individual elements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜