Hi Jain,
I'm also interested in your solution. I have a need to modify/delete text in
the middle of a large file (100s of megs), and I can't think of an easy way
of doing this,without breaking the file into parts (byte[]'s), and putting
it back together, modifing the text, and putting it back together

Thanks,
Dave

----- Original Message -----
From: "Girish Jain" <[EMAIL PROTECTED]>
To: <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM>
Sent: Friday, November 18, 2005 6:22 AM
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 DevelopMentorĀ®  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