Here's a couple ideas for you.

1.  I know this is not what you asked, but why not write a viewer that can
show the lines in the log in reverse?

2.  I think all the streaming and file io functions assume a fixed start of
the file with allocation going forward.  You'll probably have to write your
own allocation routines and buffering to make this work.  I'd look around
for the source of some of the defrag utilities that are out there.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Girish Jain
Sent: Friday, November 18, 2005 7:22 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Writing data to the beginning of a file

Hi All,

I am writing a log file with the requirement that the latest entry in the
log should be on the top. For the purpose I looked for ways of doing that
but ended up by reading the entire contents of the existing file into a byte
array and then re-write the file with the new content. Is there any better
way possible?

The current code

// Firstly read the existing file and take the contents
// into a byte array
//------------------------------------------------------------
fs = new FileStream(m_strFilePath, FileMode.Open, FileAccess.Read);
byte[] arrOriginalText = new byte[fs.Length];
fs.Read(arrOriginalText, 0, arrOriginalText.Length);
fs.Close(); fs = null;
//------------------------------------------------------------


// Now write the data to the file by re-creating the new file
//------------------------------------------------------------
fs = new FileStream(m_strFilePath, FileMode.Create, FileAccess.Write);


// Convert the new data to be written to a byte array
//------------------------------------------------------------
byte[] arrNewText = new byte[ strData.Length ];
Encoding.UTF8.GetBytes( strData, 0, strData.Length, arrNewText, 0 );


// Now write data - first the new text and then the original
//------------------------------------------------------------
fs.Write(arrNewText, 0, arrNewText.Length);
fs.Write(arrOriginalText, 0, arrOriginalText.Length);

//------------------------------------------------------------


Any suggestion would be highly appreciated

Thanks in advance

Regards,
Girish Jain

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to