473,503 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert lines of text into an existing text file?

MJ
Currently i am using the below coding to create a text file and write something on it.

Dim wfile As New System.IO.StreamWriter("c:\test.txt"
wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You"
wfile.Close(

The text file content should be like thi

Hell
Ar
Yo

What if i want to insert "How" into the text file to modify the text file become like this.

Hell
Ho
Ar
Yo

Anyone can provide the coding on how to get the specific line in a text file and insert lines of text into the text file?Any help is greatly appreciated... Thanks..
Jul 21 '05 #1
4 15076
Dim s as String = "Hello" & ControlChars.CrLf & "Are" & ControlChars.Crlf &
"You"

s.Insert(5, "How")
"MJ" <an*******@discussions.microsoft.com> wrote in message
news:BF**********************************@microsof t.com...
Currently i am using the below coding to create a text file and write something on it..
Dim wfile As New System.IO.StreamWriter("c:\test.txt")
wfile.WriteLine("Hello" & vbcrlf & "Are" & vbcrlf & "You")
wfile.Close()

The text file content should be like this

Hello
Are
You

What if i want to insert "How" into the text file to modify the text file become like this..

Hello
How
Are
You

Anyone can provide the coding on how to get the specific line in a text

file and insert lines of text into the text file?Any help is greatly
appreciated... Thanks..
Jul 21 '05 #2
MJ <an*******@discussions.microsoft.com> wrote:
Anyone can provide the coding on how to get the specific line in a
text file and insert lines of text into the text file?Any help is
greatly appreciated... Thanks..


You need to rewrite the whole file, basically. One way which doesn't
take up too much memory is to read from the file and write to a
temporary file, then delete the old file and rename the new one
appropriately. Line insertions are achieved by reading/writing some
lines, then writing the new lines, then reading/writing the rest of the
lines.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Cor
Hi MJ,

Did you look at the array.join

I hope this helps?

Copr
Jul 21 '05 #4
Cor
Hi MJ,

Looking it over and looking at the other messages than I think you can maybe
also use the.

mystring.replace(" ", vbcrlf)

And to make our C# friends happy.

I did a test this week which I have showed in the languages.vb newsgroup.

This is the fastest replace in my test and with a long string it can be more
than 100 times faster than the Vb.microsoft string function. (This in
oposite from the indexoff where the vb.microsoft function were in my tests
twice as fast with a string and twice as slow with a character).

Cor
Jul 21 '05 #5

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

Similar topics

15
7363
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
4
10693
by: Rob | last post by:
Hey, I'm pretty new to MySQL and have a question. I've built a database locally and have inputed some information. I need to go back and update some records, specifically by taking long pieces...
2
2794
by: sm | last post by:
How to "Insert" (not append) new text segment to an existing text file? Assume that we have text file as shown below; Elvis Sofia Kylix BCB--> How to insert here? Atten BuilderX Roma
2
25163
by: Nico Grubert | last post by:
Hi there, I would like to open an existing file that contains some lines of text in order to append a new line at the end of the content. My first try was: >>> f = open('/tmp/myfile', 'w')...
2
4024
by: Fang | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
16
16975
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
9
3660
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
7
2087
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
8
6463
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
0
7287
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
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
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,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.