Solved

Truncate and Append file in C#

Posted on 2004-08-20
6
7,970 Views
Last Modified: 2008-02-26
Hi All,

Can someone please give me some sample code that would show how to truncate one file by 512K and then append 511K back to the original file from the last 511K of a another source file.  In psuedocode here is what I am trying to do:

//Get handle to the file to truncate
FileInfo f = new FileInfo("c:\\FiletoTruncate.txt");
FileStream s = f.Open(FileMode.Truncate);

// Need help here, how do I truncate by 512K?????

// Get 511K of a new file an append to file above
FileInfo appendFile = new FileInfo("c:\\getLast511K.txt");

// Need help in getting the last 511K of this and appending it to the first file


0
Comment
Question by:zozig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852902
The System.IO.FileMode.Truncate flag opens the file for writing, but deletes all the data... it truncates the file to 0 bytes only.

I suggest that you:

 1. Open the file and read all but the last 512K of data into a byte[] buffer.
 2. Read the 511K Segment to another buffer.
 3. Truncate the file.
 4. Write the first buffer to the file.
 5. Write the 511K segment to the file.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852953
1.

  System.IO.FileInfo truncateFileInfo;
  truncateFileInfo = new System.IO.FileInfo("c:\\FiletoTruncate.txt");

  System.IO.FileStream fileStream;
 
  fileStream = truncateFileInfo.Open(System.IO.FileMode.Open);
  byte[] truncateBuffer;
  using(fileStream)
  {
    truncateBuffer = new byte[fileStream.Length - 0x80000];
    fileStream.Read(truncateBuffer, 0, truncateBuffer.Length);
  }
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852983
2. Read your 511K segment into another buffer (called appendBuffer), and then finally:
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 10

Expert Comment

by:eternal_21
ID: 11852990
3., 4., 5.

  fileStream = truncateFileInfo.Open(System.IO.FileMode.Truncate);
  using(fileStream)
  {
    fileStream.Write(truncateBuffer, 0, truncateBuffer.Length);
    fileStream.Write(appendBuffer, 0, appendBuffer.Length);
  }
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 11853456
Second attempt is a little cleaner...  Doesn't require you to load massive files into memory...

### C#.NET ###

  const string TRUNCATE_FILENAME = "c:\\FiletoTruncate.txt";
  const int TRUNCATE_LENGTH = 0x80000;

  const string APPEND_FILENAME = "c:\\getLast511K.txt";
  const int APPEND_LENGTH = 0x7FC00;

  System.IO.FileStream truncateFileStream;
  truncateFileStream = new System.IO.FileStream(TRUNCATE_FILENAME, System.IO.FileMode.Open);
 
  using(truncateFileStream)
  {
    byte[] appendBuffer;

    System.IO.FileStream appendFileStream;
    appendFileStream = new System.IO.FileStream(APPEND_FILENAME, System.IO.FileMode.Open);
    using(appendFileStream)
    {
      appendFileStream.Seek(-APPEND_LENGTH, System.IO.SeekOrigin.End);
      appendBuffer = new byte[APPEND_LENGTH];
      appendFileStream.Read(appendBuffer, 0, APPEND_LENGTH);
    }

    truncateFileStream.SetLength(truncateFileStream.Length - TRUNCATE_LENGTH);
    truncateFileStream.Seek(0, System.IO.SeekOrigin.End);
    truncateFileStream.Write(appendBuffer, 0, APPEND_LENGTH);
  }

###
0
 
LVL 1

Author Comment

by:zozig
ID: 11861518
eternal_21,

Thanks for the quick response, this was exactly what I was looking for.

Zozgi
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# winforms programmitically move panels 6 53
IEnumerable<T> to a List<T> 8 53
Problem to Office 1 45
create dataset from text files to go on winforms gridview 3 26
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question