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 8751695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:05:53+00:00 2026-06-13T13:05:53+00:00

I have this code, which adds a line into my Students.txt, however, everytime I

  • 0

I have this code, which adds a line into my Students.txt, however, everytime I compile and run the code, it does it again. What code can I add so that the new record is only added once?

string newLastName = "'Constant";
string newRecord = "(LIST (LIST 'Constant 'Malachi 'D ) '1234567890 'mdconstant@mail.usi.edu 4.000000 )";
string line;
string lastName;
bool insertionPointFound = false;

for (int i = 0; i < lines.Count && !insertionPointFound; i++)
{
    line = lines[i];
    if (line.StartsWith("(LIST (LIST "))
    {
        values = line.Split(" ".ToCharArray());
        lastName = values[2];
        if (newLastName.CompareTo(lastName) < 0)
        {
            lines.Insert(i, newRecord);
            insertionPointFound = true;
        }
    }
}

if (!insertionPointFound)
{
    lines.Add(newRecord);                          //This record is always added, making the file longer over time 
                                                    //if it is not deleted each time from the Students.txt file in 
}                                                  //the bin folder.

File.WriteAllLines("Students.txt", lines);
  • 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-13T13:05:54+00:00Added an answer on June 13, 2026 at 1:05 pm

    If what you are writing is known or only to occur in the file once, then first open the file and check if the record-to-be-written exists in the file. If it doesn’t, then proceed to write it, otherwise don’t write it.

    if (!File.ReadAllText("students.txt").Contains(newRecord))
    {
      // write to file...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which adds a x number of views to a Multiview
I have this code, which creates an image, and then adds some effects to
I have this code which does the trick: #include <stdio.h> int main() { int
I have this JS code which adds some HTML inside a tbody : thestring='<tr><td><input
I have this piece of code which creates a note and adds to the
I have some code which adds a Java FX WebView to a JFrame. This
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class

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.