Streamreader or Streamwriter?

jigax

Active member
Joined
Aug 17, 2006
Messages
43
Programming Experience
Beginner
Not sure what to use or how to use it. But I would like either streamreader or streamwriter to ignore a line depending on it contents. Thanks in advance.
 
When writing it's not up to the StreamWriter to ignore anything. It just writes what you tell it to. If you don't want it to write a line then don't tell it to.

As for the StreamReader, it can't ignore anything either. It just reads what's there. If you don't want to make use of what it reads then don't.

I've given you a couple of clues how you would do that. Here's a hint: they're bold and underlined. :)
 
Yes you have given me a few examples :) thank you much for them. Here is my code I think I'm doing the right thing but it's not working out how I would like it to.

VB.NET:
        Using reader As New IO.StreamReader(OpenFileDialog1.FileName.ToString)
            Using writer As New IO.StreamWriter(SaveFileDialog1.FileName.ToString)
                Dim line As String
                line = reader.ReadLine()

                Do Until reader.EndOfStream

start:
                    line = reader.ReadLine
                    line = Replace(line, "Brief Desc: Tech Assigned: ", "")
                    line = Replace(line, "ORDER TYPE: Sales Order ORDER SUB TYPE: ", "")

                    line = Replace(line, ", ", " ")


                    If line.StartsWith("ACCOUNT:") Then
                        line = Replace(line, " ", ",")
                        writer.Write(line)
                        GoTo start
                    End If

                    'Write the field value.
                    writer.Write(line)

                    If line.StartsWith("Service Order ") Or line.StartsWith("Halsted Communications ltd. ") Or line.StartsWith("13 Commerce Drive ") Or line.StartsWith("Ballston Spa, NY 12020 ") Then
                        'Write a line terminator.
                        writer.WriteLine()
                    Else
                        'Write a field terminator.
                        writer.Write(",")
                    End If

                Loop
            End Using
        End Using
        MsgBox("Done")

It will ignore Service Order but not the rest.
 
First up, do you really want that first call to ReadLine before the Do loop? That means that you reading and then discarding the first line of the file. Is that really what you want?

Secondly, get rid of that GoTo and NEVER use it again. Instead of this:
VB.NET:
    Do
label:
        If something
            DoSomething()
            GoTo label
        End If

        DoSomethingElse()
    Loop
you can do this:
VB.NET:
    Do
        If something
            DoSomething()
            Continue Do
        End If

        DoSomethingElse()
    Loop
but this would be better:
VB.NET:
    Do
        If something
            DoSomething()
        Else
            DoSomethingElse()
        End If
    Loop
As for the question, you aren't ignoring anything. You're writing the line BEFORE you test what it starts with, so every line will get written. Like I said, If you don't want a line to be written then you don't call WriteLine.
 
Maybe you should post your input file and your output file and we can comment on how to make one look like the other
 
Is there a way to format or split a line? for example i have a line with City State Zip i would like to put a , in between each so that it shows on seperate columns. But my problem is that not all lines have space. My first thought was replace space with , but it will screw other things up. Thanks
 
VB.NET:
line.Insert(line.IndexOf("State"), ",")
line.Insert(line.IndexOf("Zip"), ",")
Or
VB.NET:
line.Replace(" State", ",State")
line.Replace(" Zip", ",Zip")
 
The example also doesn't have to be used exactly as is. You can change it to suit your needs. Take the first example, for instance... Get the index of whatever it is you want to put the comma behind. Then maybe add a "-1" or "+ String.Length" to it. Make it suit your needs.
 
I'm still a bit confuse i will give you an example. below is a sample txt file that i'm converting to csv by adding a , and then writer.WriteLine() fot every service order.

Service Order
sensitive data****
sensitive data****
sensitive data****
sensitive data****
NEW BRITAIN, CT 06053 <----- could change to any city, state zip
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****
sensitive data****

And here is the code.

VB.NET:
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        Using reader As New IO.StreamReader(OpenFileDialog1.FileName.ToString)
            Using writer As New IO.StreamWriter(SaveFileDialog1.FileName.ToString)
                Dim line As String

                line = reader.ReadLine()
                line = reader.ReadLine()
                line = reader.ReadLine()
                line = reader.ReadLine()

                Do Until reader.EndOfStream

                    'start:
                    line = reader.ReadLine
                    line = Replace(line, "xxxxxx", "")
                    line = Replace(line, "xxxxxx", "")

                    'line = Replace(line, ", ", " ")

                    If line.StartsWith("xxx xxx-xxxx ") Then
                        line = reader.ReadLine
                        line = reader.ReadLine
                    ElseIf line.StartsWith("xxxxx ") Then
                        line = reader.ReadLine
                    ElseIf line.StartsWith("xxxxx") Then
                        line = reader.ReadLine
                        line = reader.ReadLine
                    ElseIf line.StartsWith("xxxxx") Then
                        line = reader.ReadLine
                        line = reader.ReadLine
                        line = reader.ReadLine
                        line = reader.ReadLine
                    ElseIf line.StartsWith("xxxxx") Then
                        line = Replace(line, " ", ",")
                        writer.Write(line)
                    ElseIf line.StartsWith("xxxxx") Then
                        writer.Write(Strings.Left(line, 10))
                        line = reader.ReadLine
                        writer.Write(",")
                    ElseIf line.StartsWith("xxxxxx") Then
                        line = reader.ReadLine
                        line = reader.ReadLine
                    End If

                    'Write the field value.
                    writer.Write(line)

                    If line.StartsWith("xxxxx") Then

                        'Write a line terminator.
                        line = reader.ReadLine
                        line = reader.ReadLine
                        line = reader.ReadLine
                        writer.WriteLine()
                    Else

                        'Write a field terminator.
                        writer.Write(",")
                    End If

                Loop
            End Using
        End Using
        MsgBox("Done")

    End Sub
I replaced sensitive data on the sample txt with "sensitive data****" and "xxx" on the code.
 
VB.NET:
Dim line As String = "NEW BRITAIN, CT 06053"
line.Insert(line.LastIndexOf(" "), ",")
Or
VB.NET:
Dim line As String = "NEW BRITAIN, CT 06053"
Dim StringArray As String() = line.Split(" ")
Dim NewString As String = ""
For I As Byte = 0 To StringArray.Length - 1
    If I = StringArray.Length - 1 Then
        NewString &= ", " & StringArray(StringArray.Length - 1)
    ElseIf I = 0 Then
        NewString &= StringArray(I)
    Else
        NewString &= " " & StringArray(I)
    End If
Next

First is easier and nicer... Second is more flexible (You can add commas at any point).

Both are untested. They look correct to me, but may require some touch-up.
 
Sivvy

Strings are immutable, your first code is a non-op. You must capture the return value. For modifying strings you should use a stringbuilder

Your second example is somewhat more horrendous from a performance viewpoint; you definitely should use a stringbuilder. Also, there are some code variable naming conventions I'd question, and the use of a byte rather than an integer for the counter


jigax, you'd be better off performing a regex replacement, and tailoring the regular expression to match state (always 2 letters) and zip (always N numbers?)
 
I understand everything you've said, but... Just out of curiosity, what's wrong with using Bytes if you know the number will be Unsigned and less than 255? I am still learning, so it would be beneficial to know.

As for the naming conventions, it's only because it's an example... And the StringBuilder Class is something I use often, but didn't think it would be necessary with appending less than 5 strings per line.
 
what's wrong with using Bytes if you know the number will be Unsigned and less than 255?
Do you know that?


As for the naming conventions, it's only because it's an example...
So, when teaching someone something, it doesnt strike you as being the best time to follow conventions? :)

And the StringBuilder Class is something I use often, but didn't think it would be necessary with appending less than 5 strings per line.
Any time youre appending more than once to a string, use a stringbuilder
 
Back
Top