Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:49:54+00:00 2026-06-14T04:49:54+00:00

I saw some previous posts related to opening a file to perform read and

  • 0

I saw some previous posts related to opening a file to perform read and write operations but I din’t get an answer for my task. I want to append some results to a file (should create a new file if it does not exist).
However, if the file already has result then append should be skipped and move on to next search for next result. I wrote a script for this and I have problem in reading the file.
The script is something like this:

proc example {} {
    set a [result1 result2  ... result n]
    set op [open "sample_file" "a+"]
    set file_content ""
    while { ![eof $op] } {
        gets $op line
        lappend file_content $line
    }
    foreach result $a {
        if {[lsearch $file_content $result] == -1} {
            puts $op $result
        }
    }
    close $op
}

Note: In this script I find variable “line” to be empty {“”}. I guess I have trouble in reading the file. Please help me with this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:49:55+00:00Added an answer on June 14, 2026 at 4:49 am

    What you forgot, is to seek to the beginning of the file before reading:

    proc example {} {
        set a {result1 result2  ... result n}; # <== curly braces, not square
        set op [open "sample_file" "a+"]
        set file_content ""
        seek $op 0; # <== need to do this because of a+ mode
        while { ![eof $op] } {
            gets $op line
            lappend file_content $line
        }
        foreach result $a {
            if {[lsearch $file_content $result] == -1} {
                puts $op $result
            }
        }
        close $op
    }
    

    Update

    You can simplify the reading (while loop and all), with one single read statement:

    proc example {} {
        set a {result1 result2  result3}
        set op [open "sample_file" "a+"]
        seek $op 0
        set file_content [read $op]
        foreach result $a {
            if {[lsearch $file_content $result] == -1} {
                puts $op $result
            }
        }
        close $op
    }
    
    example
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw some documentation and previous questions about this, but couln't find a solution.
I saw some code when studying the open source project: here . But I
I saw some code on an unrelated question but it got me curious as
I saw some other similar questions on this topic here but they were not
I saw some answers for how to upload file with the traditional form, I
I saw some similar questions to this but none seems to address this is
I saw some examples with both of them, but I don't understand where to
i saw some info regarding this question in several threads but non suited my
I saw some websites that (for example): if you want to view your message
I saw some html that displays the results of a PHP code just by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.