Format a Text file

dameron

Member
Joined
Dec 5, 2005
Messages
7
Programming Experience
3-5
I what to take a .txt file and break each line down to only 30 characters per line and the text that is after the 30 chars. drop it down into the next line. So what it would do is, take a text file grab all of the text and make a new text file with only 30 chars per line but have all of the orignal text in it.

Can someone help me on this. I have searched for a couple of days and have found nothing.

Thanks,dameron
 
VB.NET:
        Dim myString As String                          'The last line read.
        Dim myStrings As Specialized.StringCollection   'The collection of strings.

        'Continue until no lines remain.
        Do Until myStreamReader.Peek() = -1
            'Read the next line.
            myString = myStreamReader.ReadLine()

            Do
                If myString.Length > 30 Then
                    'Add the first thirty characters to the collection.
                    myStrings.Add(myString.Substring(0, 30))

                    'Remove the first thirty characters.
                    myString = myString.Substring(30)
                Else
                    'Add the remaining characters to the collection.
                    myStrings.Add(myString)
                    Exit Do
                End If
            Loop
        Loop
Then you just write every string in the collection back out to the file, which you would do with a StreamWriter.
 
jmcilhinney said:
You haven't told me what the error is. Why?

Have you used the code as is or have you made changes other than the variable names?

Sorry I'm trying to Write the collection to a file and can't seem to get it

When i tried to use the code as is it was saying that myStreamReader was not declared. So when I declared it
Dim myStreamReader As StreamReader = File.OpenText("C:\test.txt")
Then that was throwing the error on
myStrings.Add(myString.Substring(0, 30))

Could I get you to show me how to get the text file and write it back out?
 
You still haven't told me what the error message is. As for writing back to the file, you just open a StreamWriter and loop through the collection calling WriteLine.
 
jmcilhinney said:
You still haven't told me what the error message is. As for writing back to the file, you just open a StreamWriter and loop through the collection calling WriteLine.


An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
 
jmcilhinney said:
Ah, I declared the collection but didn't instantiate it. Add the "New" keyword to the StringCollection declaration to actually create an instance.

Thanks for the code.... Now when I loop through the collection I only get the last string. Could you help me with the looping of the collection. I'm new with VB.Net and just trying to learn. thanks again for all the help
 
When you say "looping through the collection" I assume you mean to write to the file. You obviously need to Open a StreamWriter first and Close it afterwards, but to actually write the file is three lines of code:
VB.NET:
For Each myString As String In myStringCollection
     myStreamWriter.WriteLine(myString)
Next myString
 
jmcilhinney said:
When you say "looping through the collection" I assume you mean to write to the file. You obviously need to Open a StreamWriter first and Close it afterwards, but to actually write the file is three lines of code:
VB.NET:
For Each myString As String In myStringCollection
     myStreamWriter.WriteLine(myString)
Next myString

Read Below to see correction.......................................
 
Last edited:
Don't take the code I post as literal. It is just an example. You have no variable named myStringCollection. You have declared a variable named myStrings that is your StringCollection object. Also, you're declaring a variable named myString in the For Each loop for the second time. You already have a variable named myString so you can use it without the "As String" part in the loop. Assume that when I post code it is an example of the principles involved. It's up to you to then adapt it to your own situation.
 
Thanks for all of the help.Below is the working code to take a text file and cut the Characters down to 30 chars per line. I'm posting this code to help others that are trying to learn. jmcilhinney thanks for being so cool with me and helping me learn a little. Hope this helps someone else....

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'The last line read.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myStrings [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Specialized.StringCollection [/SIZE][SIZE=2][COLOR=#008000]'The collection of strings.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myStreamReader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamReader = File.OpenText("C:\test.txt")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myStreamWriter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamWriter
myStreamWriter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter("C:\test2.txt")
 
[/SIZE][SIZE=2][COLOR=#008000]'Continue until no lines remain.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Until[/COLOR][/SIZE][SIZE=2] myStreamReader.Peek() = -1
[/SIZE][SIZE=2][COLOR=#008000]'Read the next line.
[/COLOR][/SIZE][SIZE=2]myString = myStreamReader.ReadLine()
[/SIZE][SIZE=2][COLOR=#0000ff]Do
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] myString.Length > 30 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Add the first thirty characters to the collection.
[/COLOR][/SIZE][SIZE=2]myStrings.Add(myString.Substring(0, 30))
[/SIZE][SIZE=2][COLOR=#008000]'Remove the first thirty characters.
[/COLOR][/SIZE][SIZE=2]myString = myString.Substring(30)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Add the remaining characters to the collection.
[/COLOR][/SIZE][SIZE=2]myStrings.Add(myString)
[/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Do
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Loop
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Strings [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] myStrings
myStreamWriter.WriteLine(Strings)
[/SIZE][SIZE=2][COLOR=#008000]'Label1.Text = myString
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] Strings
 
myStreamWriter.Close()
[/SIZE]
 
Back
Top