Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Streamreader/writer problem

HungGarHungGar Posts: 2Member
I've been googling this problem for a while and I can't find an answer, I'm writing a program that basically looks at a text file, reads a line, parses it out, and writes to another file. I'm VERY new and my knowledge is mostly self-taught, but I can't seem to get out of this one. It works for smaller files, but it appears to reach a limit in characters at some point because in a file of 900 lines, it stops writing about halfway through 800 and there are no errors, the program actually completes and the message box pops up.

here is the code, hopefully someone can help. I'm sure there are a lot of problems with how i wrote this.

Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim IndexValues As String()
Dim TextPath As String = txtFilePath.Text.ToString
Dim TextFile As StreamReader
Dim FinalFile As StreamWriter
Dim GarbagePath As String()
Dim i As Integer
Dim ID As Integer = 1
If File.Exists(TextPath) Then
TextFile = File.OpenText(TextPath)
End If
FinalFile = File.CreateText(TextPath & ".csv")
FinalFile.WriteLine("ID,Company Number,File Cabinet,Invoice Number,Supplier Code,Filename")
Do Until TextFile.Peek() = -1
IndexValues = TextFile.ReadLine().Split(",")
GarbagePath = IndexValues(IndexValues.GetUpperBound(0)).Split("")
For i = 0 To IndexValues.GetUpperBound(0)
IndexValues(i) = Replace(IndexValues(i), Chr(34), "")
GarbagePath(GarbagePath.GetUpperBound(0)) = Replace(GarbagePath(GarbagePath.GetUpperBound(0)), _
Chr(34), "")
Next
FinalFile.WriteLine(ID & "," & IndexValues(3) & "," & IndexValues(5) & "," & IndexValues(7) & "," & IndexValues(9) _
& "," & GarbagePath(GarbagePath.GetUpperBound(0)))
ID += 1
Erase IndexValues
Loop
MessageBox.Show("Invoices Complete!")
End Sub

Comments

  • nick2304nick2304 Posts: 9Member
    Im not sure what you are asking for. Do you want the program to read a specific line and parse just that one line out to another file, or do you want the program to copy all the contents from one file to another?
  • HungGarHungGar Posts: 2Member
    The code is opening the text file and saving the entire text as a streamreader, then it is, line by line, parsing it and writing it to another file. The problem is there seems to be some kind of limit in characters or whatever, I can't tell. The program "completes" but when I look at the final text file, it is cut off.
  • DataDinkDataDink Posts: 135Member
    Your ultimate problem is that you are not "Flushing" the FinalFile stream writer. Flush will force a write of the remaining data in the writer to the file.

    There are some other issues in your code that I noticed also. If you want, I've written a slightly revised version below...

    For this code you will want to import System.IO
    [code]
    Dim TextPath As String = "C:Temp.csv" ' replace me with your control's value
    If (File.Exists(TextPath) = False) Then Return ' If no file then don't continue

    Dim IndexValues As String()
    Dim GarbagePath As String ' Don't really need this...
    Dim ID As Integer = 1

    Dim TextFile As StreamReader = File.OpenText(TextPath)
    Dim FinalFile As StreamWriter = File.CreateText(TextPath & ".result.csv") ' output file
    FinalFile.WriteLine("ID,Company Number,File Cabinet,Invoice Number,Supplier Code,Filename") ' header

    While (TextFile.EndOfStream = False) ' Was there a reason you were using peek?
    IndexValues = TextFile.ReadLine().Replace(Chr(34), String.Empty).Split(",") ' Is there a reason to not strip " from the whole line?
    If (IndexValues.Length < 10) Then Continue While ' This will catch blank lines

    GarbagePath = Path.GetFileName(IndexValues.Last()) ' System.IO already has this functionality built in...
    FinalFile.WriteLine(ID & "," & IndexValues(3) & "," & IndexValues(5) & "," & IndexValues(7) & "," & IndexValues(9) & "," & GarbagePath)
    ID += 1
    End While

    TextFile.Dispose() ' remember to always dispose of these
    FinalFile.Flush() ' This is probably where your problem was - you have to flush these little guys
    FinalFile.Dispose()
    MessageBox.Show("Invoices Complete!")

    [/code]
Sign In or Register to comment.