.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 749
Number of posts: 1301

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Streamreader/writer problem Posted by HungGar on 8 Jul 2009 at 10:00 AM
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
Report
Re: Streamreader/writer problem Posted by nick2304 on 8 Jul 2009 at 1:59 PM
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?
Report
Re: Streamreader/writer problem Posted by HungGar on 9 Jul 2009 at 6:26 AM
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.
Report
Re: Streamreader/writer problem Posted by DataDink on 9 Jul 2009 at 2:26 PM
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
        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!")





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.