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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:36:32+00:00 2026-05-26T02:36:32+00:00

I am trying to read a text file in C# and add line numbers

  • 0

I am trying to read a text file in C# and add line numbers to the lines.

This my input file:

    This is line one
    this is line two
    this is line three

And this should be the output:

    1 This is line one
    2 this is line two
    3 this is line three

This is my code so far:

class Program
{
    public static void Main()
    {
        string path = Directory.GetCurrentDirectory() + @"\MyText.txt";

        StreamReader sr1 = File.OpenText(path);

        string s = "";

        while ((s = sr1.ReadLine()) != null)           
        {
            for (int i = 1; i < 4; i++)
                Console.WriteLine(i + " " + s);
            }

            sr1.Close();
            Console.WriteLine();    
            StreamWriter sw1 = File.AppendText(path);
            for (int i = 1; i < 4; i++)
            {
                sw1.WriteLine(s);
            }

            sw1.Close();               
    }
}

I am 90% sure I need to use for cycle to get the line numbers there but so far with this code I get this output in the console:

1 This is line one
2 This is line one
3 This is line one
1 this is line two
2 this is line two
3 this is line two
1 this is line three
2 this is line three
3 this is line three

And this is in the output file:

This is line number one.
This is line number two.
This is line number three.1 
2 
3 

I am not sure why the string variable s is not used when writing in the file even though it is defined earlier (another block, another rules maybe?).

  • 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-26T02:36:33+00:00Added an answer on May 26, 2026 at 2:36 am
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    namespace AppendText
    {
        class Program
        {
            public static void Main()
            {
                string path = Directory.GetCurrentDirectory() + @"\MyText.txt";
    
                StreamReader sr1 = File.OpenText(path);
    
    
                string s = "";
                int counter = 1;
                StringBuilder sb = new StringBuilder();
    
                while ((s = sr1.ReadLine()) != null)
                {
                    var lineOutput = counter++ + " " + s;
                    Console.WriteLine(lineOutput);
    
                    sb.Append(lineOutput);
                }
    
    
                sr1.Close();
                Console.WriteLine();
                StreamWriter sw1 = File.AppendText(path);
                sw1.Write(sb);
    
                sw1.Close();
    
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a text file line by line, and add each line
I am trying to read a text file line by line, and the text
I am trying to read in a text input file that contains a list
I am trying to read a large text file (14MB), each line into a
I am trying to read some info from a text file by using windows
I am trying to read values from a text file using the Qt code
I'm trying to read a list of items from a text file and format
I'm trying to read the contents of a small text file using the common
I'm just trying to do a simple text file read in java for my
I'm trying to have my application read through a text file and look for

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.