开发者

Problem with skipping lines

开发者 https://www.devze.com 2023-02-19 10:27 出处:网络
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

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

精彩评论

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

关注公众号