473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inserting text at the beginning of an existing text file

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.
Jan 23 '06 #1
4 3416
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/de...classtopic.asp

I hope this helps,

Cor
Jan 23 '06 #2
AHP
Thanks, I know about stringbuilder. I was still hoping for a more efficient
way though.

"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/de...classtopic.asp

I hope this helps,

Cor

Jan 23 '06 #3
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
Jan 23 '06 #4
AHP
Thanks Andrew, this can work. I'll change the submission algorithm to cater
for it. Thanks. - AHP.

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.


Jan 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tuuska | last post by:
Hello! I have this problem, when fwrite() writes to the beginning of xyz.txt file, it overwrites the first line. Any ideas how to prevent this? I'm running PHP 4.3.3RC3 on Linux. <?php $fp =...
1
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes...
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
8
by: Eddie Suey | last post by:
I want to add a new line to the begining of a text file. I dont want to write over existing data. How do I do this? the file is about 7 mb.
2
by: Clark Stevens | last post by:
Hi. This should be so easy, but I don't get it. Let say I have RichTextbox1 and I want to insert some text at the current insertion point, or at the beginning of selected text (if there is any). ...
11
by: Mellow Crow | last post by:
I had a problem in IE 6 when trying to insert a table using W3C DOM techniques. I found a solution and share it. :) Initially I had...... ********************** <!DOCTYPE html PUBLIC...
2
by: C# to XML | last post by:
Hi friends, I am new to csharp and XML : fp=File.OpenText(server.mappath(".\\upload\\")+"test.txt"); string info=fp.readtoend(); string arinfo =new string; char splitter = {'|','^'}; ...
3
by: japi | last post by:
Hi, i would like to know what approach i should use to insert (not append) a line of text to the begin of an existing text file. My current approach works, but i am afraid of loosing data if...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.