VB 2008 - Reading a text file and printing each line (console)?
2010-04-18 08:30:29 UTC
I am making a console application that needs to be able to print the contents of a text file, one line at a time.
My attempt at this has failed - it prints a single, empty line.
I cannot find anywhere online where I can learn how to do this, so does anybody know?
Three answers:
KyleUndefined
2010-04-18 10:06:37 UTC
This is what I did in one of my projects, it's using .NET 4 but should be able to use it with a few minor tweaks.
Private Sub ReadLines(ByVal fileName As String)
Dim str() As String = File.ReadAllLines(fileName)
Using sw As New IO.StreamWriter(fileName)
For Each Line As String In str
sw.WriteLine(Line)
Next
End Using
End Sub
2016-12-12 09:13:17 UTC
you should apply string manipulation. truthfully, you search for each and each letter in the line for the "|" separator and any be conscious that comes earlier that separator is archives. something like this: dim i as integer, j as integer do even as( i < str.length() and mid(str, i, a million) <> "|" ) i +=a million 'i = i + a million loop textbox1.textual content = mid(str, a million, i) then proceed on... j = i do even as( j < str.length() and mid(str, j, a million) <> "|" ) j += a million loop textbox2.textual content = mid(str, i, j) probable no longer the precise thanks to do it, notwithstanding it quite is the truthfully element i ought to imagine of.
peteams
2010-04-18 09:49:26 UTC
Ignoring the "one line at a time" part of your request, and assuming we're in C# (not that any of the other .NET languages are much different) you could write:
string whole = File.ReadAllText("MyFile.txt");
Console.Write(whole);
Taking into account you're "one line at a time" because you probably did mean it:
string[] lines = File.ReadAllLines("MyFile.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.