4 Replies - 2019 Views - Last Post: 27 April 2008 - 01:47 PM Rate Topic: -----

#1 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Reading From txt file

Posted 27 April 2008 - 10:50 AM

Hi,

I have one program writeing numbers to a txt file. I have a second reading this text file and displaying the numbers.
So if the text file has 5 on one line and 15 on the 2nd the code below displays
5
15
in the list box.

How can i get it to read the 1st line display it in listbox1 and display the 2nd line in listbox2

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim fileContents() As String = System.IO.File.ReadAllLines("C:\encrypt.txt")
		For i As Integer = 0 To fileContents.GetUpperBound(0)
			fileContents(i) = System.IO.Path.GetFileNameWithoutExtension(fileContents(i))
		Next
		ListBox1.Items.AddRange(fileContents)
	   
	End Sub
  
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Reading From txt file

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: Reading From txt file

Posted 27 April 2008 - 11:27 AM

This is simple validation. Read in a line and either based on a counter you setup or the type of information read, route it to the appropriate control.

How you validate it will be up to you. If you want all even numbers going to one listbox and all odd going to another, you would simply setup a counter and do something like...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileContents() As String = System.IO.File.ReadAllLines("C:\\test.txt")
        For i As Integer = 0 To fileContents.GetUpperBound(0)

            fileContents(i) = System.IO.Path.GetFileNameWithoutExtension(fileContents(i))

            ' If counter is even, send line to listbox1
            ' Otherwise it is odd and send line to listbox2
            If i Mod 2 = 0 Then
                ListBox1.Items.Add(fileContents(i))
            Else
                ListBox2.Items.Add(fileContents(i))
            End If
        Next

End Sub



Notice that we check the value of "i" to see if it is even (that is if you divide it by 2 and it returns a remainder of 0 it is even) then we send the content to one listbox, otherwise we send it to the other listbox.

This is just one way to validate and redirect the content. You can read the actual value and if it is greater than 10 send it to one list box otherwise send it to the other.

It us up to you how you split it up. Enjoy!

"At DIC we be work splitting code ninjas... we split up work as 1% actual work and 99% break time" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: Reading From txt file

Posted 27 April 2008 - 11:57 AM

Hi Martyr2,

What i want is that what ever is in the first line in the text file to go into the first listbox or textbox and what is in the 2nd line of the textfile to go into the 2nd listbox or textbox.

As later i might add a 3rd and 4th line which i will need to go into listbox 3 and listbox4.

Also do you know how i can add to the text file. As currently in the first program that creates the textfile it sends textbox1.text to line 1 in the txt file and textbox2.text to line 2. But if i run the program again it overwrites them.

I want to be able to run the program save the data, run it again later and instead of overwriting just add to line 3 and line 4

This post has been edited by smelf1: 27 April 2008 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: Reading From txt file

Posted 27 April 2008 - 01:17 PM

Quote

What i want is that what ever is in the first line in the text file to go into the first listbox or textbox and what is in the 2nd line of the textfile to go into the 2nd listbox or textbox.

As later i might add a 3rd and 4th line which i will need to go into listbox 3 and listbox4.


Yes I got this much already, I told you the answer. You can divide and conquer based on a number of criteria. I gave you one type of criteria right there. The answer remains the same... validate the data and send to the appropriate controls.

Quote

I want to be able to run the program save the data, run it again later and instead of overwriting just add to line 3 and line 4


This all depends on how you open up the file. If you open the file in append mode, you can add onto the file. If you need to add onto specific lines that already exist, read in the line append what data you want, then rewrite the whole file to include the new lines that you just concatenated to.

Take a look at how you can open files, you will notice a filemode parameter in the FileStream object that allows you to open a file in append mode. The filemode enum has various modes, one being "Append"

But like I said if you want to add to existing lines later, you can read in the lines, append to them, then just rewrite the file. How you do all this is dependent on your program design.

:)
Was This Post Helpful? 0
  • +
  • -

#5 smelf1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 19-March 08

Re: Reading From txt file

Posted 27 April 2008 - 01:47 PM

Thanks Martyr,

This is all new to me and i have no clue about programming. Below is how i write to the file atm.

   Private Sub ButtonSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveFile.Click
		'Saves Public Key N&M into encrypt.txt file.
		Using writer As New System.IO.StreamWriter("C:\Encrypt.txt")
			writer.WriteLine(Me.TextBoxPubN.Text)
			writer.WriteLine(Me.TextBoxPubE.Text)
		End Using
		'Saves Private Key N&D into decrypt.txt file.
		Using writer As New System.IO.StreamWriter("C:\Decrypt.txt")
			writer.WriteLine(Me.TextBoxPrivN.Text)
			writer.WriteLine(Me.TextBoxPrivD.Text)
		End Using
	End Sub

End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1