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

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 />
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.

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.

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.

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"

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).