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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:41:13+00:00 2026-05-16T05:41:13+00:00

I want to append two text files together. I have one file with a

  • 0

I want to append two text files together.

I have one file with a carriage return line feed at the end. Observe file A which is 28 bytes.

this is a line in the file\n

then I have another file which is the same thing without the new line. Observe file B which is 26 bytes.

this is a line in the file

I want to append the same file to itself (file A to A, and file B to B) and compare the byte counts.

However, when using StreamReader.ReadLine() on file A, I get a value returned but MSDN says:

A line is defined as a sequence of characters followed by a line feed (“\n”), a carriage return (“\r”) or a carriage return immediately followed by a line feed (“\r\n”). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached.

However, there is no crlf in the file.

How can I safely append these files without adding an extra line break at the end? For example, StreamWriter.WriteLine() will put an extra line break on file A when I don’t want it to. What would be an ideal approach?

  • 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-16T05:41:13+00:00Added an answer on May 16, 2026 at 5:41 am

    StreamReader and StreamWriter (which derive from TextReader and TextWriter) are not suitable for situations requiring an exact form of binary data. They are high level abstractions of a file which consists of bytes, not text or lines. In fact, not only could you wind up with different number of newlines, but depending on the environment you might write out a line terminator other than the expected CR/LF.

    You should instead just copy from one stream to another. This is quite easy actually.

    var bytes = File.ReadAllBytes(pathIn);
    var stream = File.Open(pathOut, FileMode.Append);
    stream.Write(bytes, 0, bytes.Length);
    stream.Close();
    

    If the size of the file is potentially large, you should open both the input and output file at the same time and use a fixed-sized buffer to copy a block at a time.

    using (var streamIn = File.Open(pathIn, FileMode.Read))
    using (var streamOut = File.Open(pathOut, FileMode.Append)) {
    
        var bytes = new byte[BLOCK_SIZE];
    
        int count;
        while ((count=streamIn.Read(bytes, 0, bytes.Length)) > 0) {
            streamOut.Write(bytes, 0, count);
        }
    
    }
    

    Also worth noting is that the above code could be replaced by Stream.CopyTo which is new in .NET 4.

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

Sidebar

Related Questions

If I want to append the contents of a src file into the end
I have a binary file to which I want to append a chunk of
I have a directory with 260+ text files containing scoring information. I want to
I have two lines in a text file like below: S<Switch_ID>_F<File type> _ID<ID number>_T<date+time>_O<Original
I have two columns in a text file. I read them into Python into
I want to append the contents of two rich text boxes in a Windows
I have two divs one inside of the other, now i want to put
I want to append more text behind UILabel in iOS. In other languages, we
In my application, I want to append cells to the current tableview and have
I have an input text file which I am reading and storing everything in

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.