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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:07:13+00:00 2026-06-11T11:07:13+00:00

It does not matter if I read a TXT file or a XML file

  • 0

It does not matter if I read a TXT file or a XML file I always see “extra” info added into my file which is saved to disk. We first do the following:

FileStream fs = new FileStream(fileMoverFile.SourcePath, FileMode.Open, FileAccess.Read);

Then we assign fs to a variable of type Stream which we pass to the function below:

private void SaveToDisk(Stream fileStream, string saveToPath)
{
  if (!Directory.Exists(Path.GetDirectoryName(saveToPath)))
  {
    Directory.CreateDirectory(Path.GetDirectoryName(saveToPath));
  }
  FileStream outputStream = new FileInfo(saveToPath).OpenWrite();
  const int bufferSize = 1024;
  byte[] buffer = new byte[bufferSize];
  int bytesRead = fileStream.Read(buffer, 0, bufferSize);
  while (bytesRead > 0)
  {
    outputStream.Write(buffer, 0, bufferSize);
    bytesRead = fileStream.Read(buffer, 0, bufferSize);
  }
  outputStream.Close();
}

When I open the file which was saved to disk, I see extra information which is basically some content of the same file being repeated with some other info which do not belong to the file.
Very strange.

What could be causing this?

  • 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-11T11:07:14+00:00Added an answer on June 11, 2026 at 11:07 am

    You need to write bytesRead bytes, not bufferSize bytes:

    int bytesRead = fileStream.Read(buffer, 0, bufferSize);
    while (bytesRead > 0)
    {
        outputStream.Write(buffer, 0, bytesRead); // Change this here
    

    Right now, when you reach the end of the input stream, you’re potentially writing more data than you read in, which will cause “extra garbage” at the end of the file.

    That being said, if your goal is just to copy the stream, you could just use Stream.CopyTo (provided you’re in .NET 4+). This avoids the read/write loop entirely, and simplifies your code dramatically:

    private void SaveToDisk(Stream fileStream, string saveToPath)
    {
      if (!Directory.Exists(Path.GetDirectoryName(saveToPath)))
      {
        Directory.CreateDirectory(Path.GetDirectoryName(saveToPath));
      }
      using(FileStream outputStream = new FileInfo(saveToPath).OpenWrite())
      {
          fileStream.CopyTo(outputStream);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a text stored in db, file, resource does not matter: <code><%=
It does not matter what programming language. Can anyone please give me an idea
According to the Puppet documentation: Order does not matter in a declarative language. If
I need a simple parser generator. The language does not matter. It only has
In what scenarios we should go for <dl> not <ul> ? Does it matter
does not work in js file while it works perfectly in jsp file. I
I'm trying to read a line from a static file and insert it into
Premise: I am parsing a file that is quite nearly XML, but not quite.
I want to be able to read in a file, myFile.txt , that I
CLOCK_MONOTONIC does not seem available, so clock_gettime is out. I've read in some places

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.