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

  • Home
  • SEARCH
  • 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 6138247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:50:58+00:00 2026-05-23T17:50:58+00:00

I am trying to go line by line through text file using stream reader,

  • 0

I am trying to go line by line through text file using stream reader, and when I encounter specific text, I want to add a new line into that file. But when I do so, next line’s first part(about half) gets cut off. I want to do that only once, that’s why “skip” variable is there. Here is what I am doing:

                var fileStream = new FileStream(fileName, FileMode.Open);
                var writer = new StreamWriter(fileStream);
                var reader = new StreamReader(fileName);
                string line;
                var skip = false;
                using (writer) {
                    using (reader) {
                        while ((line = reader.ReadLine()) != null) {
                            if (line.Contains("some text") && !skip) {
                                writer.WriteLine("some text something else");
                                skip = true;
                            }
                        }
                    }
                }
                fileStream.Close();

Am I doing something wrong here?

  • 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-05-23T17:50:58+00:00Added an answer on May 23, 2026 at 5:50 pm

    EDIT: Aargh, I’ve just seen that you’re trying to read and write to the same file.

    You shouldn’t do that – it’s replacing the existing text, rather than inserting extra data. You can’t just insert data within a file – that’s not how file systems work.

    Instead, read from one file, write to another file, and then move the files around afterwards if you need to.

    I’d also suggest that you have using statements for the whole scope of a variable, rather than using them the way you have here. Something like this:

    void CopyAndEdit(string inputFile, string outputFile)
    {
        // In .NET 4 you can use File.ReadLines which will
        // return an IEnumerable<string>
        using (TextReader reader = File.OpenText(inputFile))
        {
            using (TextWriter writer = File.CreateText(outputFile))
            {
                bool editing = true;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    writer.WriteLine(line);
                    if (editing && line.Contains("some text"))
                    {
                        editing = false;
                        writer.WriteLine("some text something else");
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a disorganized text file into a list suitable for
I'm trying to read in a text file line by line and process each
Using VB.NET, I am trying to create a text file if it doesn't exist
I am trying to send an email (through gmail) using python script that someone
I'm trying to write a script that will search through a html file and
I am trying to take a line of text like 13) Check for orphaned
I'm using a text file to save the changes made by a user on
Trying to write a code that searches hash values for specific string's (input by
I am trying to automate DNS zone creation by using a batch file fired
I’m trying to get my account's time line and display it. I’m using Abraham

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.