Read Entire File

Simcoder

Well-known member
Joined
Apr 18, 2003
Messages
124
Location
Texas
What is the easiest way to read an entire text file with multiple lines one
character at a time.

Example, lets say the text file has 3 lines, containing the following letters below, how would I read in each indivdual character(including spaces) into
a variable and when it reaches the end of the file, last character (z), make
the process stop. I know theres an easy way to achieve this, any help
would be much appreciated.

+-------------------+
| TextFile1.txt
| a b c d e f g h i j
| k l m n o p q r s t
| u v w z y z
+-------------------+


-=Simcoder=-
 
when it reaches the end of the file, last character (z),
umm, EOF, and last character(z) = EOF ????
Code:
static string GetText(string filename)
{
	System.IO.TextReader rdr = System.IO.File.OpenText(filename);
	try
	{
		return rdr.ReadToEnd();
	}
	finally
	{
		txt.Close();
	}
}
 
Try this:
Code:
Dim filePath As String = "C:\test.txt"
Dim streamReader1 As New IO.StreamReader(filePath)

 read the entire file into one variable
Dim strFileText As String = streamReader1.ReadToEnd

 if you want to separate each line, then use a string array
 every element of the stringLines array will be a line in the file
Dim strLines() As String = Split(strFileText, vbCr)

 close the stream
streamReader1.Close()

Hope this is what you were looking for.
 
Unless you must read it one byte at a time, Id read in the whole string and loop through each char, one at a time. Something like the following:
C#:
string s = System.IO.File.OpenText("c:\\test.txt").ReadToEnd();
foreach(char ch in s.ToCharArray())
{
}

-ner
 
Back
Top