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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:03:04+00:00 2026-06-01T20:03:04+00:00

I am trying to read a large text file (14MB), each line into a

  • 0

I am trying to read a large text file (14MB), each line into a list of strings, then get the distinct strings out of it, then write it back to another text file, I use the following code:

    static void removeDuplicates(string filename)
    {
        //Reading from the file
        Console.WriteLine("Reading from the file....");
        StreamReader sr = new StreamReader(filename);
        List<string> namesList = new List<string>();
        while (!sr.EndOfStream)
        {
            namesList.Add(sr.ReadLine());
        }

        //Getting the distinct list
        namesList=namesList.Distinct().ToList<string>();           


        Console.WriteLine("Writing to the new file");
        //writing back to the file
        StreamWriter sw = new StreamWriter(filename + "_NoDuplicates",false);
        for (int i = 0; i < namesList.Count; i++)
        {               
            sw.Write(namesList[i] + "\r\n");

        }
    }

The problem is the streamWriter always stops writing after certain number of lines, always stops writing at the same place

I made sure that the List contents are right, and that the loop goes through all the items in the list, it’s just the streamWriter problem.

The list contains 1048577 items before the Distinct(), and 880829 after the Distinct();

The streamWriter stops writing in the middle of the string number 880805 and doesn’t write anything after that, it even stops in the middle of a word !

Why is that happening, what am I doing wrong?

  • 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-01T20:03:05+00:00Added an answer on June 1, 2026 at 8:03 pm

    If you’re not getting an error then my guess is that the last bit of the file is still buffered. Try adding a call to sw.Flush() to the end of your method.

    And, of course, you need to close the stream, which should flush the buffer anyway.

    Explanation

    The StreamWriter internally uses a buffer. Every time you call Write() the data is actually written to the buffer in memory. When the buffer fills up it gets flushed to the disk.

    The problem you were seeing is because the last few lines of the file you’re writing didn’t fill up the buffer, so there was no trigger to flush the buffer to disk. It always occurs at the same point in the file because that is the last whole-number of multiple of the size of the buffer. By closing the stream you cause any remaining data to be flushed to disk.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read in a large text file (~2.5 MB) into my
I am trying to read a large list of English words from a text
We are trying to read each word from a text file and replace it
I am trying to read an extremely large text file. I want to write
I'm trying to find out what is the best way to read large text
I'm trying to read in a 150mb text file into a Rich Text box.
I am trying to read a 12MB+ file which has a large HTML table
I have memory mapped a large formatted (text) file containing one integer per line
I'm trying to process a large text file through a HttpServlet (tomcat). As this
I'm currently trying to read the contents of an XML file into a Map

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.