read/write line by line a text file in vb.net
Page 1 of 110 Replies - 469 Views - Last Post: 06 December 2012 - 09:09 AM
#1
read/write line by line a text file in vb.net
Posted 03 December 2012 - 11:45 PM
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
Replies To: read/write line by line a text file in vb.net
#2
Re: read/write line by line a text file in vb.net
Posted 04 December 2012 - 02:02 AM
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.
#3
Re: read/write line by line a text file in vb.net
Posted 04 December 2012 - 03:00 AM
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?
#4
Re: read/write line by line a text file in vb.net
Posted 04 December 2012 - 03:50 AM
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.
#5
Re: read/write line by line a text file in vb.net
Posted 04 December 2012 - 05:08 AM
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
#6
Re: read/write line by line a text file in vb.net
Posted 04 December 2012 - 06:02 AM
#7
Re: read/write line by line a text file in vb.net
Posted 05 December 2012 - 10:41 PM
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
#8
Re: read/write line by line a text file in vb.net
Posted 06 December 2012 - 05:22 AM
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
#9
Re: read/write line by line a text file in vb.net
Posted 06 December 2012 - 08:46 AM
thanks alot
just please give me a full code
i do use from your guide so thank you again
i'm waiting
#10
Re: read/write line by line a text file in vb.net
Posted 06 December 2012 - 08:57 AM

POPULAR
#11
Re: read/write line by line a text file in vb.net
Posted 06 December 2012 - 09:09 AM
Thank u
|
|

New Topic/Question
Reply



MultiQuote



|