Home All Groups Group Topic Archive Search About

Inserting text at the beginning of an existing text file

Author
23 Jan 2006 8:19 AM
AHP
Hi,
I'm using Visual Studio 2005. I am developing a web application that uses
the FileUpload control to upload text files to a directory on a webserver.
This works fine.

However, for me to be able to further process the files, I need to insert
some data at the BEGINNING of the text file. All the solutions I've seen
either append the text to the end of the file, or overwrite the data at the
start of the file, or read the entire file into a string
("StreamReader.ReadToEnd"), concatenate, then write the string back to the
file (innefficient).

Please note:
The text files in question could be up to 5000 lines long with each line
being up to 200 characters long (+- 1 million chars), and storing all that in
a string may be a strain on resources, so the solution must be efficient.

Isn't there a way to directly access the file to do the insert?? Something
like:
1. Convert text file to a "direct access file"
2. Insert data at the begining
3. Convert "direct access file" back to a text file

So far my code is as follows for the submit button on the page (see my
commented lines):

Imports System
Imports System.IO

....

Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles butSubmit.Click

.... (working code)

    If FileOK Then
        Try
            Dim strNewFileName As String = path & Now().Ticks.ToString &
"NewFileName.txt"
            Dim strRefNo As String = "1234567890"

            Me.FileUpload1.PostedFile.SaveAs(strNewFileName)

'The text file now contains:
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.

'this is where I need a code snippet .....
'----
'---- INSERT strRefNo at the BEGINNING of the text file represented by
strNewFileName ----
'----

'After the insert, the text file must be on the webserver and now contain:
'1234567890
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.

            Me.lblResult.ForeColor = Drawing.Color.Black
            Me.lblResult.Text = "File successfully uploaded!"
        Catch ex As Exception
            Me.lblResult.ForeColor = Drawing.Color.Red
            Me.lblResult.Text = "File could not be uploaded."
        End Try
    Else
        'display error message
        Me.lblResult.ForeColor = Drawing.Color.Red
        Me.lblResult.Text = "Cannot accept files of this type."
    End If 'FileOK

.... (working code)

End Sub

Any help will be appreciated.

Author
23 Jan 2006 8:50 AM
Cor Ligthert [MVP]
AHP,

Whatever you do with a string in Net, does mean that it will be copied
completely. The word is immutable.

To prevent that is the stringbuilder. (Although in your case a lot will be
copied).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasstopic.asp

I hope this helps,

Cor
Author
23 Jan 2006 9:04 AM
AHP
Thanks, I know about stringbuilder. I was still hoping for a more efficient
way though.

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> AHP,
>
> Whatever you do with a string in Net, does mean that it will be copied
> completely. The word is immutable.
>
> To prevent that is the stringbuilder. (Although in your case a lot will be
> copied).
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextstringbuilderclasstopic.asp
>
> I hope this helps,
>
> Cor
>
>
>
Author
23 Jan 2006 10:33 AM
Andrew Morton
AHP wrote:
> However, for me to be able to further process the files, I need to
> insert some data at the BEGINNING of the text file. All the solutions
> I've seen either append the text to the end of the file, or overwrite
> the data at the start of the file, or read the entire file into a
> string ("StreamReader.ReadToEnd"), concatenate, then write the string
> back to the file (innefficient).

The file system doesn't work like that. Compare it with adding a new page of
writing to the start of a notebook. If you'd left space before starting
writing on the first page you could have done it, but then you'd likely have
some empty pages between the new page and the old writing. (If you knew
exactly how much you were going to add, that wouldn't be a problem.)

Write new data to new file
Append old data to new file
Rename old file (just in case something goes wrong)
Rename new file to old filename
Confirm operation worked
If worked, delete old file.

- OR -

Give each file a sequence number and process them in sequence. It will be
faster to rename a few files than rewrite them.

Andrew
Author
24 Jan 2006 5:54 AM
AHP
Thanks Andrew, this can work. I'll change the submission algorithm to cater
for it. Thanks. - AHP.

Show quoteHide quote
>
> Write new data to new file
> Append old data to new file
> Rename old file (just in case something goes wrong)
> Rename new file to old filename
> Confirm operation worked
> If worked, delete old file.
>