VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,475 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,744 people online right now. Registration is fast and FREE... Join Now!




Have StreamWriter write to the same location the StreamReader is on

 

Have StreamWriter write to the same location the StreamReader is on

Sim_270

12 Sep, 2007 - 08:00 AM
Post #1

New D.I.C Head
*

Joined: 27 Aug, 2007
Posts: 8


My Contributions
hi I have one html file which i am reading through textreader.While reading when it comes to <body> tag i want to write a image tag but not able to do so.............please help

User is offlineProfile CardPM
+Quote Post


m2s87

RE: Have StreamWriter Write To The Same Location The StreamReader Is On

12 Sep, 2007 - 09:14 AM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 15 times
Dream Kudos: 1225
My Contributions
1. To process data, read the data to memory !!!
2. Find "<body>" and replace it with "<body>" & "<img something_here><img>"
3. Save the data
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Have StreamWriter Write To The Same Location The StreamReader Is On

12 Sep, 2007 - 09:32 AM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well to read and write at the same time you will have to go to a base class of the streamreader and streamwriter... filestream. From there you can then open a file with readwrite file access and read/write content. Here is how it is done...

CODE

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click

        Dim stream As FileStream

        ' Open file for reading and writing simultaneously
        stream = New FileStream("c:\\testing.txt", FileMode.Open, FileAccess.ReadWrite)

        ' Lets move to position four from the beginning of the file
        stream.Seek(4, SeekOrigin.Begin)

        ' Using an encoding (text files), read the textbox (text1) and write to stream at position specified above.
        Dim uniEncode As New System.Text.ASCIIEncoding
        stream.Write(uniEncode.GetBytes(text1.Text), 0, uniEncode.GetByteCount(text1.Text))

        ' Lets setup a byte array to read some of our text
        ' Make sure your byte array is big enough, here we only read about 10 test characters so 256 is enough.
        Dim ByteArray(256) As Byte

        ' Reset our position back to beginning of file
        stream.Position = 0

        ' Read the stream and place in array from 0 position onwards
        stream.Read(ByteArray, 0, 256)

        ' Close stream
        stream.Close()

        ' Show what was read
        MessageBox.Show(uniEncode.GetString(ByteArray))


    End Sub


The in code comments should explain it all. For your situation you will just need to find out where in the stream <body> is and then after it, write in your image text and close. That will put in your image.

Enjoy!

decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:21AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month