Link to home
Start Free TrialLog in
Avatar of websuperman
websuperman

asked on

Streamwriter problem. It wont write anything to the file

It keeps telling me my file is being already being used, but it is creating it.

Here is my code:

 Sub Main()

        Dim sr = IO.File.OpenText("earnings.txt")
        Dim dataline() As String

        Do
            dataline = sr.readline.split(","c)

            Dim Plantcode = dataline(0).Trim
            Dim Deptnum = dataline(1).Trim
            Dim Empnum = dataline(2).Trim
            Dim Lname = dataline(3).Trim
            Dim Fname = dataline(4).Trim
            Dim Earnings = CDbl(dataline(5))
            Dim Ytdearnings = CDbl(dataline(6))
            Dim currentplant As String = Plantcode
            Dim plantearnings As Double
            Dim plantytdearnings As Double
            Dim sw As IO.StreamWriter = System.IO.File.CreateText("outputfile.txt")
            Dim reportearnings As Double
            Dim reportytdearnings As Double

            If Plantcode <> currentplant Then
                sw.WriteLine(plantearnings, plantytdearnings)
                currentplant = Plantcode
                plantearnings = Earnings
                plantytdearnings = Ytdearnings
                Printheadings(Plantcode)
            Else
                plantearnings += Earnings
                plantytdearnings += Ytdearnings
            End If
            reportearnings += Earnings
            reportytdearnings += Ytdearnings
            sw.WriteLine(" ")
            sw.WriteLine(String.Format("{0,-45}{1,11:C}{2,12:C}", _
            "PLANT TOTALS FOR " & currentplant, plantearnings, plantytdearnings))
            sw.WriteLine(" ")
            sw.WriteLine(" ")
        Loop Until sr.peek = -1

        sr.close()

    End Sub
    Sub Printheadings(ByVal plant As String)
        Dim sw As IO.StreamWriter

        sw.WriteLine("{0,-40}{1,15}", "MT MANUFACTURING", Now.ToShortDateString())
        Sw.writeline("PLANT EARNINGS REPORT")
        Sw.Writeline()
        If plant = "ATL" Then
            Sw.writeline("ATLANTIC PLANT")
        ElseIf plant = "CTL" Then
            Sw.writeline("CENTRAL PLANT")
        ElseIf plant = "MTN" Then
            Sw.Writeline("MOUNTAIN PLANT")
        Else
            Sw.writeline("PACIFIC PLANT")
        End If
        sw.WriteLine("")
        sw.WriteLine(String.Format("{0,-7}{1,-7}{2,-20}{3,-12}{4,11}{5,11}", _
        "PLANT", "DEPT", "EMP NUM", "NAME", "CUR EARN", "YEAR-T-D"))
        sw.WriteLine("")

    End Sub

End Module

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial