none
Sequential File processing in VB 2010

    Question

  • Is there an easy way to read in a comma deliminated file in VB 2010.  In VB 6.0 you could read the file in directly to variables.  It appears this has been removed in 2010.

    Monday, April 02, 2012 4:12 PM

Answers

  • If you might have lines like

    1,2,"Hello, World!"

    then splitting at a comma isn't going to work and you might want to look at using a TextFieldParser.

    --
    Andrew

    Monday, April 02, 2012 5:48 PM
  • No it's still there.  There are a multitude of better ways to do it though.

    From the help files:

    FileOpen(1, "TESTFILE", OpenMode.Output)
    Write(1, "hello")
    Write(1, 14)
    FileClose(1)
    Dim s As String = "teststring"
    Dim i As Integer
    FileOpen(1, "TESTFILE", OpenMode.Input)
    Input(1, s)
    MsgBox(s)
    Input(1, i)
    MsgBox(i)
    FileClose(1)

    • Marked as answer by putrwizrd Monday, April 02, 2012 9:25 PM
    Monday, April 02, 2012 9:19 PM

All replies

  • Try this:

    Dim lines = (From line In IO.File.ReadAllLines("file path") Select line.Split(","c)).ToArray

    Result is a jagged array, which is an array of arrays. Lines(n) refers to the n-th line (0-based), and lines(n)(m) referes to the m-th column in the n-th line.


    Armin


    Monday, April 02, 2012 4:23 PM
  • You're try this does not appear.

    Why can we know longer use open File for input as # it was much easier to explain and students to understand.

    You could then input #1, variable, variable, variable


    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 4:58 PM
  • Yes, that's possible, too. You didn't mention you want it step by step in your first post. Just wanted to show you that several things can be done with much less code nowadays and not "everything is worse". :)

    This is the long version:

    Imports System.IO
    
    Public Class Main
    
       Shared Sub Main()
          
          Dim fs As FileStream
          Dim reader As StreamReader
    
          fs = New FileStream("file path", FileMode.Open)
          reader = New StreamReader(fs)
    
          Do
             Dim Line As String
             Dim Columns As String()
    
             Line = reader.ReadLine
    
             If Line Is Nothing Then Exit Do
    
             Columns = Line.Split(","c)
             '...process Columns here...
          Loop
    
          reader.Dispose()
          fs.Dispose()
    
       End Sub
    
    End Class
    


    Armin

    Monday, April 02, 2012 5:45 PM
  • If you might have lines like

    1,2,"Hello, World!"

    then splitting at a comma isn't going to work and you might want to look at using a TextFieldParser.

    --
    Andrew

    Monday, April 02, 2012 5:48 PM
  • Which is why it'a a PITA to now do file processing for reading raw data and producting simple output for instruction of students.

    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 5:53 PM
  • That's more a limitation of CSV. 


    Armin

    Monday, April 02, 2012 6:51 PM
  • I disagree,  why should I have to read a file into an array just to process.  It's like trying to write a form letter in Assembler.  While it can be done, it's not the most effcient way to do it.  C++ does not force you to read into an array to process data so why does VB?

    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 8:02 PM
  • Then don't do it. Nobody forces you to do anything. Read line by line as shown in my second example.

    Armin

    Monday, April 02, 2012 8:09 PM
  • Your second example takes much more code and creates an array of each line read in.  

    I want to be able to  Open "filename" for input as #1  then be able to, as an example:

    Input #1, strEmpName, dblHoursWorked, dblHourRate, dblTaxRate

    but it appears that this is no longer allowed


    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 8:15 PM
  • Not everyone is creating Web based Applications, were object orientated programming makes more sense.

    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 8:17 PM
  • I disagree,  why should I have to read a file into an array just to process.  It's like trying to write a form letter in Assembler.  While it can be done, it's not the most effcient way to do it.  C++ does not force you to read into an array to process data so why does VB?

    Maurice Lab Adjunct Faculty

    Where did you get this. Read first what a CSV file is before you write things which gives a very clear view about your knowledge of this topic.

    http://en.wikipedia.org/wiki/Comma-separated_values


    Success
    Cor

    Monday, April 02, 2012 8:21 PM
  • Not everyone is creating Web based Applications, were object orientated programming makes more sense.
    Can you reason that? I don't know what you mean.

    Armin

    Monday, April 02, 2012 8:26 PM
  • "Input #1,..." works because it expects a proprietary file format. Which reason do you have to create this limitation?


    Armin

    Monday, April 02, 2012 8:28 PM
  • You can't believe evrything you read.  All sequential files in the mainframe environment are technically CSV file, better know as Comma Deliminated file.  The name change to comma seperated files is actually a relatively new term in the computer industry.  Comma Separated Files, as they are now know are still comma deliminated files and can be processed as such. The comma is the separator/deliminator between two different types of information, hence in a program that reads comma deliminated files, the comma is why you can pull a complete record in on one read.  The end of the line has a CRLF character to represent the end of the lrecord.

    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 8:35 PM
  • In VB 6.0 Input #1 equates to the physical file on the hard drive that you are referencing.  The #1 is just the reference indicator to the file.  It can actually be any number one wants to choose.


    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 8:37 PM
  • In VB 6.0 Input #1 equates to the physical file on the hard drive that you are referencing.  The #1 is just the reference indicator to the file.  It can actually be any number one wants to choose.

    I know what it does. I'm using VB since VBDOS 1.0. In VB.Net, FS is the "handle" to the file.

    But the point was about the file format, not about the "#1".


    Armin

    Monday, April 02, 2012 8:44 PM
  • Is there an easy way to read in a comma deliminated file in VB 2010.  In VB 6.0 you could read the file in directly to variables.  It appears this has been removed in 2010.

    Yes, there is... an extremely simple way:

    Dim tfp As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\file.csv")
    tfp.SetDelimiters(",")
    While Not tfp.EndOfData
        Dim fields() As String = tfp.ReadFields
        'process fields
    End While
    

    Works with flat files too with a minor change to the configuration (call SetFieldWidths instead of SetDelimiters).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 02, 2012 8:50 PM
  • If you might have lines like

    1,2,"Hello, World!"

    then splitting at a comma isn't going to work and you might want to look at using a TextFieldParser.

    --
    Andrew


    Sorry Andrew, missed that you already mentioned the TextFieldParser... too much squabbling.  =P

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 02, 2012 8:54 PM
  •    Dim fields() As String = tfp.ReadFields

    The OP doesn't want to read it into an array because "Input #1,..." can read it directly into single variables.

    Just wanted to mention. :)


    Armin


    Monday, April 02, 2012 8:54 PM
  • Well, then its a good thing that fields() is an array of "single variables", huh?  ;) ;)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 02, 2012 9:01 PM
  •    Dim fields() As String = tfp.ReadFields

    The OP doesn't want to read it into an array because "Input #1,..." can read it directly into single variables.

    Just wanted to mention. :)


    Armin


    Armin, 

    Is it not, the OP thinks ........................................................... However the ReadLine method gives also the same idea.

    http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx 

    It processes line by line, but that does not mean it reads line by line from disk. It does that from the read buffer.


    Success
    Cor


    Monday, April 02, 2012 9:12 PM
  • You're right, no use continuing as you guys don't seem to understand why I want to be able to do it. 

    Consider it closed.


    Maurice Lab Adjunct Faculty

    Monday, April 02, 2012 9:18 PM
  • No it's still there.  There are a multitude of better ways to do it though.

    From the help files:

    FileOpen(1, "TESTFILE", OpenMode.Output)
    Write(1, "hello")
    Write(1, 14)
    FileClose(1)
    Dim s As String = "teststring"
    Dim i As Integer
    FileOpen(1, "TESTFILE", OpenMode.Input)
    Input(1, s)
    MsgBox(s)
    Input(1, i)
    MsgBox(i)
    FileClose(1)

    • Marked as answer by putrwizrd Monday, April 02, 2012 9:25 PM
    Monday, April 02, 2012 9:19 PM
  • @Cor:

    Probably no sense in continuing at this point... we are obviously not having a reasonable discussion here.


    Yea I've removed it. 

    :-)


    Success
    Cor

    Monday, April 02, 2012 9:21 PM
  • You're right, no use continuing as you guys don't seem to understand why I want to be able to do it. 

    Consider it closed.


    Maurice Lab Adjunct Faculty

    You never wrote that. so how would we understand. 

    You seem to miss that although you have always thought you were reading one line, you were doing that from a buffer.

    Current computers don't care anymore about the size of a textfile and therefore all that vintage way of processing is in fact senseless. You can read the the file in memory yourself. Split it and use it.

    Although in another message I gave already the link to this one.

    http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

     Which does the same as your VB6 one.


    Success
    Cor

    Monday, April 02, 2012 9:27 PM
  • Your second example takes much more code and creates an array of each line read in.  

    I want to be able to  Open "filename" for input as #1  then be able to, as an example:

    Input #1, strEmpName, dblHoursWorked, dblHourRate, dblTaxRate

    but it appears that this is no longer allowed


    Maurice Lab Adjunct Faculty

    Ok I gotta say it...

    Don't you think you're doing more harm than good by trying to teach out-dated coding techniques?  If I paid for a course only to find out later that most of what I was taught is not how it is done in modern programming, I would want my money back.

    How are you teaching logic flow?  GOTO's and line labels?  *shudders*


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 02, 2012 9:29 PM
  • @putrwzrd:

    John's post is a valid contribution to your statement that you can no longer open a file using Input.

    However, it in no way answers the primary question of the thread.  This is not an easy way to read a CSV in VB.Net...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 02, 2012 9:31 PM
  • I just wrote a very lengthy elaboration about type safety, code reusage etc pp.  I've deleted it. :)

    I narrow it down to one single question: How can I read from a NetworkStream using the Input statement in VB6?

    Again it's all about abstraction. That costs on a small scale but pays on a large scale.


    Armin



    • Edited by Armin Zingler Monday, April 02, 2012 10:06 PM formatted
    Monday, April 02, 2012 10:03 PM