file reading and writing methods ?

Trevster344

Member
Joined
Mar 15, 2011
Messages
8
Location
Austin, Tx
Programming Experience
Beginner
I'm trying to write a program that will read a text file, and sort of filter out some things into some new files for myself. I have a website that I like to put information such as Artists, and songs for music. My website writes the Artist, and Song I input to one file on a new line in the format of (Artist - Song). The problem is it's gotten massive! I want to start organizing my artists, and songs anyway I come up with but I don't even know where to begin. To narrow it down I want to go into the file grab the artists name before the (-) and then write it to one file. After that I want to write everything after the (-) which is the songs to a different file. What file reading, and writing method should I use? As you might be able to tell I'm quite the noob. Anyways any tips, or suggestions are appreciated. :) I'm going to continue digging around, and tinkering with different methods to see what I can figure out for the job.
 
Okay, yes I posted code. Sorry if this takes the fun out of it. This is one of many ways to solve your problem. It does not include checking for the edge cases or any of the other fun stuff like exceptions, improperly formatted file, etc. For example, it will not work properly if your song title or artist contains a "-", it'll only write the stuff before it.

VB.NET:
        ' Substitute the following paths for your actual ones. Using statement ensures
        ' resources are disposed of, so you don't accidentally leave a file open you can't edit.
        Using sr As System.IO.StreamReader = New System.IO.StreamReader("YourPathHere"),
            sw1 As System.IO.StreamWriter = New System.IO.StreamWriter("ArtistFile"),
            sw2 As System.IO.StreamWriter = New System.IO.StreamWriter("TitleFile")

            Dim line As String

            ' Read the entire file.
            While sr.Peek() <> -1

                ' Read the next line.
                line = sr.ReadLine()

                ' Split the line up into an array. Element0 will always be artist. Element1 will always be song.
                Dim splitLine() As String = line.Split("-"c)

                ' Write artist to new line on artist file
                sw1.WriteLine(splitLine(0).Trim)

                ' Write song to new line on song file.
                sw2.WriteLine(splitLine(1).Trim)

            End While

        End Using
 
Well here's an update. I've been tinkering a lot with string manipulation, and I haven't gotten back around to the creation of files, with the specific strings I need. I've hit a snag though. You see in the file that has the Artist - song, there is actually a (-) character. So I was trying to read the file, then index the (-), and retrieve the string that followed the (-). Here's what I've got so far, but this became a little fruity when I ran it.

VB.NET:
    Private Sub Begin()
        Try
            Dim sPath As String = txtname.Text
            'Dim c As Char = txtchar.Text
            Dim objStreamReader As streamreader
            objStreamReader = sfile.OpenText(sPath)
            If ProgressBar1.Value <> 100 Then
                Do
                    Dim sString As String = objStreamReader.ReadLine()
                    Dim sstartline As String = objStreamReader.ReadLine().IndexOf("-")
                    Dim sendline As String = objStreamReader.ReadLine().IndexOf("-")
                    Dim slength As String = objStreamReader.ReadLine().Length
                    Dim sString2 As String = Mid(sString, sstartline, slength)
                    ProgressBar1.Value += 1
                    'MsgBox(sString2)
                    MsgBox(sString2)
                Loop Until objStreamReader.EndOfStream
                'Dim sString2fnumber As Integer = sString.IndexOf(c)

                objStreamReader.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
The code is quite messy, and I apologize for that. I'm just a little bit goo goo ga ga for some of the controls I run into when I get distracted like the progress bar, and such lol. I also have a bunch of commented out statements due to continuous tinkering with different string manipulation methods, and functions. I'm looking to read from the (-) in the file to the end of the file. If anyone could give me some tips, or hints on this snag I would appreciate it once more. :)

btw I can't give a definite length to jump in a string read from the file because each line is different. The file has about 200 entries of Artist - Song, so I'm doing my best to detect the length, and find the appropriate character to start from.
 
Last edited:
I'm not quite following the placement of your -'s. Could you post a few lines of the file to make this more clear?

A note...Each time you call the ReadLine() method, you read another line. So your code reads 3 lines then takes the length of a fourth. Is this actually what you want to do?

One other comment... Mid() is a VB6 function, VB.NET offers string.Substring instead. They are both supported though.
 
Example of the placement:

"Megadeth - Countdown to Extinction". This is one line of the file. I would like it to read a line starting from the - and retrieve all the information, which will be the song only, and this will loop until it reaches the end of the file. Essentially reading all of the songs, to which I will place elsewhere else, another file most likely.

Thank you, I had difficulty with the Substring, but I'll have another go at it. I had recalled using the mid function in vb6 at one point, so I thought I would give it a go. The help is really appreciated thank you.
 
My first answer should do what you are looking for. If you're having trouble with Substring, I suggest Split since it gets you two separate strings (as an array) which is what you are looking for anyways. One will contain the artist and one will contain the song title.
 
Back
Top