10 Replies - 3251 Views - Last Post: 06 December 2012 - 09:09 AM Rate Topic: -----

#1 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

read/write line by line a text file in vb.net

Posted 03 December 2012 - 11:45 PM

I need to help
i want to read line by line a text file
then each line save seperately in a listbox
so finally change some of it then save them to another text file
thank you
Is This A Good Question/Topic? 0
  • +

Replies To: read/write line by line a text file in vb.net

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: read/write line by line a text file in vb.net

Posted 04 December 2012 - 02:02 AM

Did you try to solve your problem in some way? If so, please post your code and use :code:/>
Explain where your problem is. If you don't know where to start, then take a look at IO namespace. You forgot to ask the question.
Was This Post Helpful? 0
  • +
  • -

#3 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: read/write line by line a text file in vb.net

Posted 04 December 2012 - 03:00 AM

this is my code
but it use for one digit

Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim input_file_name As String = Application.StartupPath + "\" + "input.txt"
        Dim output_file_name As String = Application.StartupPath + "\" + "output.txt"
        Dim txt_line, a, temp As String
        Dim txtarr(100) As String
        Dim counter As Integer = 0
        Dim i, num1, num2 As Integer
        '-------------------Read From The File---------------------
        If System.IO.File.Exists(input_file_name) = True Then
            Dim objreader As New System.IO.StreamReader(input_file_name)
            '-----------------------------
            Do While objreader.Peek <> -1
                txt_line = objreader.ReadLine() & vbNewLine
                Me.TextBox1.Text = Me.TextBox1.Text + txt_line
                txtarr(counter) = txt_line
                counter = counter + 1
            Loop
            '-----------------------------
            For i = 1 To Val(txtarr(0))
                temp = txtarr(i)
                num1 = temp.Substring(0, 1)
                num2 = temp.Substring(2, 1)
                Me.TextBox2.Text = Me.TextBox2.Text + (num1 + num2).ToString + vbCrLf
            Next
            '-----------------------------
        Else
            MsgBox("File Does Not Exist", vbOKOnly)
        End If
        '---------------------Write To The File------------------------------------
        If System.IO.File.Exists(output_file_name) = True Then
            Dim objwriter As New System.IO.StreamWriter(output_file_name)
            '-----------------------------
            objwriter.Write(Me.TextBox2.Text)
            objwriter.Close()
            MsgBox("Text Written To File", vbOKOnly)
        Else
            MsgBox("File Does Not Exist", vbOKOnly)
        End If
    End Sub
End Class



if my entring be unknown digit number what do i?
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: read/write line by line a text file in vb.net

Posted 04 December 2012 - 03:50 AM

For a start, you need to divide reading from file and filling it's content to listbox, from changing its values, and writing them all back to separate file (unless this is really what you are trying to do, and don't want to allow user to make any changes, only programmatically). Now, you are doing all in one shot.

Then tell us what are you trying to achieve. What do you expect your code should do. What do you have in input.txt, and what would you like to have in output.txt. Currently, your code is a mess.
Was This Post Helpful? 0
  • +
  • -

#5 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: read/write line by line a text file in vb.net

Posted 04 December 2012 - 05:08 AM

in input.txt is a number in first line that it's the count of sum operation on tow numbers in ofter second line
for example :
5---------> five sum
10 52
22 44
1 99
14 78
13 12

in output.txt i want write tow numbers at second line in input.txt
for example:
line1 62
line2 66
line3 100
line4 93
line5 25

thank u
Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: read/write line by line a text file in vb.net

Posted 04 December 2012 - 06:02 AM

I'm really sorry, but I have no idea what you are trying to achieve. Perhaps it's the language barrier, as I'm not native English speaker also. If there's someone, who understands OP's problem, please jump in.
Was This Post Helpful? 0
  • +
  • -

#7 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: read/write line by line a text file in vb.net

Posted 05 December 2012 - 10:41 PM

hi Luchy3
i want read the below entring from a text file then get another text file that it's sum of numbers in first file:

file1:input.txt
Example:
3 "this number is number of sum operation"
because it's 3 so we have three sum operation
14 6 "these numbers is operand of first sum operation"
8 13 "these numbers is operand of second sum operation"
55 36 "these numbers is operand of third sum operation"

So my output file include below :
20
21
91
Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: read/write line by line a text file in vb.net

Posted 06 December 2012 - 05:22 AM

Now I understand your problem. First of all, I think you don't need first number in input.txt, as its only purpose is to indicate number of lines in this file. You can get that by counting lines each time you read the file line-by-line. You can even decide to read just specific number of lines from file, and then stop reading it. I propose creating a function, that returns List(Of String) (or if you wish, you can return List(Of Summands), where Summands is a class (or structure) with 2 public members: FirstSummand As Double : SecondSummand As Double)...

Your function would use the logic:
    Function GetLinesFromFile(ByVal filePath As String,
                 Optional ByVal linesToRead As Integer = 0) As List(Of String)

        Dim returnList As New List(Of String)
        Dim counter As Integer = 0

        'check for file existance, and throw IOException if there's no file

        'If linesToRead = 0 Then read file to the end, and add each line to returnList
        'Else
        'Do Until counter = linesToRead, and exit Do when it is
        'again add each line read to returnList
        'increment counter: counter += 1
        'loop

        Return returnList
    End Function



Your previous code shows that you know how to read file lines...

As I mentioned, you can decide to return List(Of T) (where T is structure or class with both/all summands)... Better yet staying flexible, and returning IEnumerable(Of T), so you can consume this function by any object, that implements IEnumerable interface (like array, or List(Of T),...).

In that case you need to do a little change to above function:
  • return IEnumerable(Of Summands)
  • implement splitting read lines, and filling the result in Summands
  • adding Sumands to returnList


That's it. So the function will be something like:

    Class Summands
       Public Property FirstSummand as Double 'or integer or any other appropriate value type
       Public Property SecondSummand as Double
    End Class

    Function GetLinesFromFile(ByVal filePath As String,
                 Optional ByVal linesToRead As Integer = 0) As IEnumerable(Of Summands)

        Dim returnList As New List(Of Summands) 'change here
        Dim counter As Integer = 0

        'check for file existance, and throw IOException if there's no file

        'If linesToRead = 0 Then read file to the end
        'with each line use .Split, and read the result array, adding both values to new Summands
        'add this Summands to returnList
        'Else
        'Do Until counter = linesToRead, and exit Do when it is
        'again split each line read, read the result array, adding both values to new Summands
        'add this Summands to returnList
        'increment counter: counter += 1
        'loop

        Return returnList
    End Function



Edit:
I forgot to say something about output.txt...
With what you get returned from the above function, just iterate through it, and sum summands, add the results to some collection (like List(Of Decimal)), and when you're finished with it, write this collection to output.txt.

P.S.
If your native language is supported, use Google Translate to help yourself with English (preset languages in link are for example only).

This post has been edited by lucky3: 06 December 2012 - 05:26 AM

Was This Post Helpful? 1
  • +
  • -

#9 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: read/write line by line a text file in vb.net

Posted 06 December 2012 - 08:46 AM

Mr Luchy3
thanks alot
just please give me a full code
i do use from your guide so thank you again
i'm waiting
Was This Post Helpful? -4
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: read/write line by line a text file in vb.net

Posted 06 December 2012 - 08:57 AM

*
POPULAR

Nothing burns me more than 'I'm waiting'. Like we fucking owe you something? We're here to help, but you seriously have help confused with code it for me.
Was This Post Helpful? 5
  • +
  • -

#11 mahdir8  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 7
  • Joined: 03-December 12

Re: read/write line by line a text file in vb.net

Posted 06 December 2012 - 09:09 AM

OK
Thank u
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1