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

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

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




Read and write back to the same text file

 

Read and write back to the same text file

kayatri

2 Jul, 2009 - 09:26 PM
Post #1

D.I.C Head
**

Joined: 5 May, 2009
Posts: 190


My Contributions
I have a text file which look like this:

M48
METRIC,LZ
VER,1
FMAT,2
DETECT,ON
%
G93X0Y29
T250
X22Y0
X0Y0
T169
X016038Y11991
X203998Y11991
X203998Y-134725
T168
X216888Y105277
X216Y-108
X003148Y105277
X004Y-108
T170
X11Y1535
T176
X119035Y-126978
X024039Y-119104
X024039Y-120374
X024801Y-117961
X048118Y-124438


Now I need to do is i need to read the file after the line DETECT,ON then if i find T250,T169,T176.....Txxx which ever line start with T i must read and write that line back in between the line FMAT,2 and DETECT,ON in my same text file. Can any one help me how can i do this?

User is offlineProfile CardPM
+Quote Post


LoveIsNull

RE: Read And Write Back To The Same Text File

2 Jul, 2009 - 10:56 PM
Post #2

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
I'll be honest with you; I think I have accomplished exactly what you are asking. I did this in less than 20 lines of code.
The thing is, you've posted beyond a few threads like this, asking us to help you parse\edit some kind of text. I would just rather help guide you through this sort of thing, so you can help yourself better with it next time, than just hand you the code. And believe me, at this point (2:30 AM here), it would certainly be easier for me to just post the code.

I used a text box and then put the changes back into the text box, but you could just as easily do this from and to a file.
Either way, I put the lines of text into a string array. This was the only string array that I used. The variable that holds my output is actually a generic list of type string. If you are not sharp on Generics, read up on them, they are amazing. In this case, the Insert method really comes in handy.
The only other variable is an integer I use to hold the number of "Txxx" occurrences found, which only is important if you want them in the same order as they were in the file.

For, I will iterate through each line of the array that holds my input.
I check if the first character is a "T", and then I make sure that the next three characters are numeric. And finally, I check that the index of this line in the array is greater than the index of the line "DETECT,ON".

If the line has those three things going for it, then I will insert this line into my generic list of type string, after the index of the line "FMAT,2" within my input array and any other lines that I may have already inserted as determined by an integer variable. I will also add one to the count of this integer variable.

Or else, if the line does not have those three things going for it, I will just add this line to my generic list of type string.

I will then move on to the next line.

After this is all done, I will clear my text box and set its lines property equal to the generic list of type string, after I convert it to an array.

Now, see if you can follow those instructions and the hints to write this code by yourself. At least try and post what you've got because me or somebody else will always be willing to help. wink2.gif

This post has been edited by LoveIsNull: 2 Jul, 2009 - 11:06 PM
User is offlineProfile CardPM
+Quote Post

kayatri

RE: Read And Write Back To The Same Text File

2 Jul, 2009 - 11:41 PM
Post #3

D.I.C Head
**

Joined: 5 May, 2009
Posts: 190


My Contributions
QUOTE(LoveIsNull @ 2 Jul, 2009 - 10:56 PM) *

I'll be honest with you; I think I have accomplished exactly what you are asking. I did this in less than 20 lines of code.
The thing is, you've posted beyond a few threads like this, asking us to help you parse\edit some kind of text. I would just rather help guide you through this sort of thing, so you can help yourself better with it next time, than just hand you the code. And believe me, at this point (2:30 AM here), it would certainly be easier for me to just post the code.

I used a text box and then put the changes back into the text box, but you could just as easily do this from and to a file.
Either way, I put the lines of text into a string array. This was the only string array that I used. The variable that holds my output is actually a generic list of type string. If you are not sharp on Generics, read up on them, they are amazing. In this case, the Insert method really comes in handy.
The only other variable is an integer I use to hold the number of "Txxx" occurrences found, which only is important if you want them in the same order as they were in the file.

For, I will iterate through each line of the array that holds my input.
I check if the first character is a "T", and then I make sure that the next three characters are numeric. And finally, I check that the index of this line in the array is greater than the index of the line "DETECT,ON".

If the line has those three things going for it, then I will insert this line into my generic list of type string, after the index of the line "FMAT,2" within my input array and any other lines that I may have already inserted as determined by an integer variable. I will also add one to the count of this integer variable.

Or else, if the line does not have those three things going for it, I will just add this line to my generic list of type string.

I will then move on to the next line.

After this is all done, I will clear my text box and set its lines property equal to the generic list of type string, after I convert it to an array.

Now, see if you can follow those instructions and the hints to write this code by yourself. At least try and post what you've got because me or somebody else will always be willing to help. wink2.gif

Thank You for advise and instruction. I will try to do this
User is offlineProfile CardPM
+Quote Post

kayatri

RE: Read And Write Back To The Same Text File

3 Jul, 2009 - 05:12 PM
Post #4

D.I.C Head
**

Joined: 5 May, 2009
Posts: 190


My Contributions
I manage to read my text file after the line DETECT,ON then find T250,T169,T176.....Txxx which ever line start with T. I place that lines in temprary file then i sort the lines so that it will arrange in acending order. So far my code is like this

CODE
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim filename As String = strFileName
        Dim i As Long = 0
        Dim myReader As StreamReader = New StreamReader(filename)
        Dim myLine As String
        If File.Exists("C:\kaya.txt") Then

            Console.WriteLine("{0} already exists.", "C:\kaya.txt")
            Return
        End If

        Dim sf As StreamWriter = File.CreateText("C:\kaya.txt")


        Do
            myLine = myReader.ReadLine()
        Loop Until myLine.Contains("DETECT,ON")


        Do Until myReader.EndOfStream
            myLine = myReader.ReadLine()

          
            If myLine.Contains("T") Then

                sf.WriteLine(myLine)


            End If









        Loop

      

        myReader.Close()
        myReader = Nothing
        sf.Close()




    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim filename As String = strFileName
        Dim lines As String() = IO.File.ReadAllLines("C:\kaya.txt")
        Dim sv As StreamWriter = File.CreateText("C:\kaya.txt")
        Array.Sort(lines)

        For Each strName As String In lines

            sv.WriteLine(strName)
        Next

        sv.Close()
    End Sub




My question now is I want to copy the content of my temprary file and place it in my original file. I want to place that content in between the line FMAT,2 and DETECT,ON in original file.. Can we do this .. Can any one help me?

Thank you
User is offlineProfile CardPM
+Quote Post

LoveIsNull

RE: Read And Write Back To The Same Text File

3 Jul, 2009 - 05:40 PM
Post #5

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
icon_up.gif icon_up.gif

QUOTE(kayatri @ 3 Jul, 2009 - 07:12 PM) *

My question now is I want to copy the content of my temprary file and place it in my original file. I want to place that content in between the line FMAT,2 and DETECT,ON in original file.. Can we do this .. Can any one help me?

Thank you


This is why I used a generic list to hold my output, so I could utilize it's Insert method. The way I did this was as follows:
CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tfLines() As String = TextBox1.Lines 'System.IO.File.ReadAllLines("C:\textfile.txt")
        Dim tfOut As New List(Of String)
        'Dim values As New List(Of String)

        Dim i As Integer = 1
        For Each line In tfLines
            If line(0) = "T" AndAlso IsNumeric(line.Substring(1, 3)) AndAlso Array.IndexOf(tfLines, line) > Array.IndexOf(tfLines, "DETECT,ON") Then
                tfOut.Insert(Array.IndexOf(tfLines, "FMAT,2") + i, line)
                'values.Add(line)
                i += 1
            Else
                tfOut.Add(line)
            End If
        Next
        TextBox1.Clear()
        TextBox1.Lines = tfOut.ToArray
    End Sub


Do note that this doesn't take into consideration things like if "DETECT,ON" or "FMAT,2" were non-existent or malformed in the file, or if there were "Txxx" lines of the exact same value, but it does the trick I think.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:49PM

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