Parts of a txt File

Bigbadborris

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

Please help im really struggling with something that Im sure is fairly straight forward.
I have a text file called CustKeySorted. it has 500+ lines and each line is 581 characters long. I would like my program to select the email address from each line then write the address to a different text file called email. the email address starts at postiion 275 and is no more than 40 characters long.

Can anyone show me how to do this as this is the last part of a project for work.

Many Thanks inadvance
 
You should start by looping through the lines of the file. There are a number of ways that you can do that. Unless you have a specific reason for doing otherwise, I would suggest using File.ReadLines. It returns an IEnumerable(Of String), so you can loop over it using a For Each loop, but it doesn't read the whole file in one go so it is quite economical. You should start by just writing each line out to another file. Open a StreamWriter for the destination file, loop over the result of File.ReadLines for the source file and just write each line.

Once you can do that, then it's time to process each String between reading and writing. You say that the email address is no more than 40 characters long. Does that mean that 40 characters are reserved for it and spaces fill any unused portion or the end is indicated in some other way? Assuming the former, you can call String.Substring to get the email address from the line and String.Trim, .TrimStart or .TrimEnd to remove the excess whitespace.
 
Thank you jmcilhinney. That made alot of sense. This is what I came up with and it works like a dream

VB.NET:
Dim email As String
        Dim File_Name As String = "C:\Serenity\DRITXT\EMAIL.txt"
        Dim objWriter As New System.IO.StreamWriter(File_Name)

        For Each line As String In File.ReadLines("C:\Serenity\DRITXT\CUSTKEYA.txt")
            If line.Contains("@") Then
                email = line.Substring(274, 40).ToLower
                objWriter.WriteLine(email)

            End If
        Next line
        objWriter.Close()

Thanks again :D
 
Back
Top