File Reading and Writing

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi all

I have the follow peice of code which reads customer data from a text file, it then extracts the data in the Customer Name and email address fields. If the email address contains the @ symbol it writes the Customer Name and the email Address in to another text file. or at least its supposed to!!!

VB.NET:
' ------------------------------------------------------------------------------------------------------
        ' VARIABLES FOR TXT FILE READING AND WRITING
        Dim File_NameIN As String = "C:\DRITXT\CUSTKEYA.txt"
        Dim File_NameOUT As String = "C:\DRITXT\MAILOUT\EMAIL.txt"
        Dim objWriter As New System.IO.StreamWriter(File_NameOUT)
        ' ------------------------------------------------------------------------------------------------------
        ' VARIABLES FOR PROCESSING DATA
        Dim EmailAdd As String
        Dim Customer As String
        ' ------------------------------------------------------------------------------------------------------
        ' RETREIVES EMAIL ADDRESS FROM CUSTKEYA AND WRITES IT TO EMAIL.TXT
        For Each line As String In File.ReadAllLines(File_NameIN)
            If line.Length = 581 Then
                Customer = line.Substring(0, 30)
                EmailAdd = line.Substring(274, 59).Trim.ToLower
            End If

            If EmailAdd.Contains("@") Then
                objWriter.Write(EmailAdd & "," & Customer & ",")
            End If
        Next

        objWriter.Dispose()
        MsgBox("FILE CREATION COMPLETE", MsgBoxStyle.OkOnly)
        If MsgBoxResult.Ok Then
            Me.Show()
        End If

Problem I am having is that it only writes the email address to the file, not the customer name.
Any ideas where I am going wrong?

Many Thanks
 
I'm seeing a couple of things here: first you're checking each line for a specific length, what if the line isn't as long? What if it's longer? I highly doubt everyone's name and email address are the same number of charaters.
Following up with that, you're reading a specific number of characters (SubString(0, 30) )for the customer's name, I'm assuming who ever created the text file will have padded it to 30 characters if it's short, or truncated it to 30 if it's too long. If not then you'll need a different way of grabbing that info.
Also the email address is being read at a specific location in the file for a specific number of characters.

Would you be able to post the first 3 to 8 lines in the text file so we have a sample of what the structure is like?
 
I'm seeing a couple of things here: first you're checking each line for a specific length, what if the line isn't as long? What if it's longer? I highly doubt everyone's name and email address are the same number of charaters.
Following up with that, you're reading a specific number of characters (SubString(0, 30) )for the customer's name, I'm assuming who ever created the text file will have padded it to 30 characters if it's short, or truncated it to 30 if it's too long. If not then you'll need a different way of grabbing that info.
Also the email address is being read at a specific location in the file for a specific number of characters.

Would you be able to post the first 3 to 8 lines in the text file so we have a sample of what the structure is like?

Than you for your reply.

Unfortunatly I cant post samples from the file because it sensitive customer data.
The file was generated using a DOS based program called FilePro. Each field length is fixed i.e Customer Name is 30 characters long, Email feild is 60 characters long.
The customer name is the first field.
 
Than you for your reply.

Unfortunatly I cant post samples from the file because it sensitive customer data.
The file was generated using a DOS based program called FilePro. Each field length is fixed i.e Customer Name is 30 characters long, Email feild is 60 characters long.
The customer name is the first field.
Sure you can post a sample, just change the names, email addresses, mailing addresses, & everything else HIPAA to random jibberish like "Bob Smith, bsmith@example.com" and stuff like that.
 
Looks like it should work. Tell me if this gives any different result (it should write all lines regardless of whether they contain an @ symbol or are 581 long):

VB.NET:
' ------------------------------------------------------------------------------------------------------
        ' VARIABLES FOR TXT FILE READING AND WRITING
        Dim File_NameIN As String = "C:\DRITXT\CUSTKEYA.txt"
        Dim File_NameOUT As String = "C:\DRITXT\MAILOUT\EMAIL.txt"
        Dim newFile As New System.Text.StringBuilder()
       
        Dim emailAdd As String
        Dim customer As String

        For Each line As String In File.ReadAllLines(File_NameIN)
            'If line.Length = 581 Then
                customer = line.Remove(30).Trim
                emailAdd = line.Substring(274, 59).Trim.ToLower
            'End If

            'If EmailAdd.Contains("@") Then
                newFile.Append(emailAdd).Append(",").Append(customer).AppendLine(",")
            'End If
        Next

        File.WriteAllText(File_NameOUT, newFile.ToString())

        MsgBox("FILE CREATION COMPLETE", MsgBoxStyle.OkOnly)
        If MsgBoxResult.Ok Then
            Me.Show()
        End If

If that writes names then we know something is wonky

For example, one such wonky could be that the file has a newline character part way along a line. Let's imagine you never realised because you always looked at it in notepad with wordwrap on. Lets say that the name is on one line and the email on another:

VB.NET:
JOHN SMITH      asdfg  sdfg df ghsd fh g h fgh fg h fgh  NEWLINE
                       sdf gsdfgsdfg dfg dflkgj dlfkgj ldfkgj dfgl dkfglkdf  john.smith@host.com

Now suppose each line gets read, on the first line the variables are:
cust = "JOHN SMITH"
emailadd = ""

On the second line they are:
cust = " "
emailadd = "john.smith@..."

But only the second line is ever written because of the IF



I also swapped your code to using a stringbuilder, because it's easier to step in the debugger and see the new file getting built. Do you know how to use the debugger?

If your file is huge, go back to using a stream, and maybe even use a stream to read it too as it will lower the memory needed by the program
 
Back
Top