Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 there's 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=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

when it reaches the end of the file, last character (z),

umm, EOF, and last character(z) = EOF ????

static string GetText(string filename)
{
System.IO.TextReader rdr = System.IO.File.OpenText(filename);
try
{
	return rdr.ReadToEnd();
}
finally
{
	txt.Close();
}
}

Posted

Try this:

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.

- The only true knowledge is knowing that you know nothing.
  • *Experts*
Posted

Unless you must read it one byte at a time, I'd read in the whole string and loop through each char, one at a time. Something like the following:

string s = System.IO.File.OpenText("c:\\test.txt").ReadToEnd();
foreach(char ch in s.ToCharArray())
{
}

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...