Reading/Writing to/from text files

Visual Basic 2010 - Reading/Writig to/from text files

Page 1 of 1

7 Replies - 3333 Views - Last Post: 31 July 2012 - 09:26 AM Rate Topic: -----

#1 KatMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 12

Reading/Writing to/from text files

Posted 28 July 2012 - 01:29 PM

I am tryin to build a program that will read a pipe-delimited text file into a drop down menu and several textboxes. The idea is that it will read the first "column" of EVERY line into the drop down menu, allowing me to select which line to read into the text boxes. After selecting a value from the drop down menu, the program will then read the remaining "columns" from that line into the textboxes. How do I do this. I am very new to Visual Basic 2010 , and have not had much experince in programming other than Javascript and the little bit of Visual Basic I know (which is just snippets found from examples, which I have changed to make it donwhat I want it to do).

Is This A Good Question/Topic? 0
  • +

Replies To: Reading/Writing to/from text files

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: Reading/Writing to/from text files

Posted 28 July 2012 - 01:47 PM

First.. you look at streamreader.. that'll get your data in one line at a time... what you want to do is read *ALL* your data in to some sort of collection - be it a dataset, lists, arrays, what ever.. then apply your crazy scheme on top of that... again - read in the data with a stream reader, hold it in some collection, and manipulate the collection.. not each line of the file.
Was This Post Helpful? 0
  • +
  • -

#3 KatMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 12

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 01:54 AM

View Postmodi123_1, on 28 July 2012 - 01:47 PM, said:

First.. you look at streamreader.. that'll get your data in one line at a time... what you want to do is read *ALL* your data in to some sort of collection - be it a dataset, lists, arrays, what ever.. then apply your crazy scheme on top of that... again - read in the data with a stream reader, hold it in some collection, and manipulate the collection.. not each line of the file.

Thank you for your response, Modi, but I need more details. I already know what you just said... What I need to know is how do I manipulate it? How do I use datasets and arrays and all that? I'm not good with this stuff yet.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 07:22 AM

Quote

How do I use datasets and arrays and all that?

Man I can't teach you how to use an array or dataset in a forum post.. Sure arrays are just indexed structures of data.. and datasets are more complex structures to hold data. Do you have a book to read or something? Go investigate msdn's information.. the information in the tutorials.. etc..


Quote

What I need to know is how do I manipulate it?

I don't know what this means.

Example of base.. and I mean base array construct manipulation..

Copy of numbers.txt

Quote

87.699,83.032
107.947,102.933
81.586,77.576


        Dim foo(5) As Int32 '-- this is an array of integers with five spots to fill up the data.
        foo(0) = 1 '-- notice they start at zero index
        foo(1) = 2 '-- adding more data
        foo(4) = 5 '-- more data
        foo(5) = 77

        '-- showing what's in there
        For Each i As Int32 In foo
            Console.WriteLine(i)
        Next


        Dim bar As New List(Of String) '-- list.. a different sort of array collection.
        Dim sr As New IO.StreamReader("C:\test\Copy of numbers.txt") '-- from our source

        While Not sr.EndOfStream '-- until we hit the end of hte file
            bar.Add(sr.ReadLine()) '-- read the line and add it to the collection of strings.
        End While

        For Each i As String In bar '-- show what we got
            Console.WriteLine(i)
        Next

        '-- now all sorts of things can happen.. since we know our data is separated by comma we can split those up and for manipulation and more direct storage..

        Dim rab(bar.Count - 1, 1) As String
        Dim temp() As String '-- to hold our split up data.
        For i As Int32 = 0 To bar.Count - 1
            temp = bar(i).Split(",") '-- split says make an array from chunks of text broken around what ever delimiter you want.. in this case make two items that break around the comma.

            rab(i, 0) = temp(0)
            rab(i, 1) = temp(1)
        Next

        '-- now I can call specific chunks of data from each row!

        Console.WriteLine("first line, first column: " + rab(0, 0))
        Console.WriteLine("first line, second  column: " + rab(0, 1))


Was This Post Helpful? 0
  • +
  • -

#5 KatMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 12

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 09:02 AM

View Postmodi123_1, on 31 July 2012 - 07:22 AM, said:

Quote

How do I use datasets and arrays and all that?

Man I can't teach you how to use an array or dataset in a forum post.. Sure arrays are just indexed structures of data.. and datasets are more complex structures to hold data. Do you have a book to read or something? Go investigate msdn's information.. the information in the tutorials.. etc..


Quote

What I need to know is how do I manipulate it?

I don't know what this means.

Example of base.. and I mean base array construct manipulation..

Copy of numbers.txt

Quote

87.699,83.032
107.947,102.933
81.586,77.576


        Dim foo(5) As Int32 '-- this is an array of integers with five spots to fill up the data.
        foo(0) = 1 '-- notice they start at zero index
        foo(1) = 2 '-- adding more data
        foo(4) = 5 '-- more data
        foo(5) = 77

        '-- showing what's in there
        For Each i As Int32 In foo
            Console.WriteLine(i)
        Next


        Dim bar As New List(Of String) '-- list.. a different sort of array collection.
        Dim sr As New IO.StreamReader("C:\test\Copy of numbers.txt") '-- from our source

        While Not sr.EndOfStream '-- until we hit the end of hte file
            bar.Add(sr.ReadLine()) '-- read the line and add it to the collection of strings.
        End While

        For Each i As String In bar '-- show what we got
            Console.WriteLine(i)
        Next

        '-- now all sorts of things can happen.. since we know our data is separated by comma we can split those up and for manipulation and more direct storage..

        Dim rab(bar.Count - 1, 1) As String
        Dim temp() As String '-- to hold our split up data.
        For i As Int32 = 0 To bar.Count - 1
            temp = bar(i).Split(",") '-- split says make an array from chunks of text broken around what ever delimiter you want.. in this case make two items that break around the comma.

            rab(i, 0) = temp(0)
            rab(i, 1) = temp(1)
        Next

        '-- now I can call specific chunks of data from each row!

        Console.WriteLine("first line, first column: " + rab(0, 0))
        Console.WriteLine("first line, second  column: " + rab(0, 1))



Yeah, I guess that would be kind of hard to give that much detail on a forum post... I didn't think about that. But I do appreciate your responses, and taking the time to help. The problem i have (and I had the same problem trying to learn Javascript) is that I can find snippets of codes and stuff, but that's all I ever got from Javascript tutorials. I had searched for months trying to find explanations to each piece of code so I actally understood what it was for and how I could modify it to do what I wanted it to do, instead of what they did wih it. So far, msdn msdn has helped a bit with helping me learn to use the streamreader and streamwriter.

Again, thank you.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 09:16 AM

Again.. do you have a book? If not you should get one.
Was This Post Helpful? 0
  • +
  • -

#7 KatMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 12

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 09:21 AM

No, and I can't afford much of anything extra right now. I'll try to get one when I can... Any titles you can suggest?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8962
  • View blog
  • Posts: 33,586
  • Joined: 12-June 08

Re: Reading/Writing to/from text files

Posted 31 July 2012 - 09:26 AM

find something off amazon that has decent reviews.. is recent.. and the index looks like it covers your knowledge gaps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1