Simcoder Posted November 16, 2004 Posted November 16, 2004 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=- Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
HJB417 Posted November 17, 2004 Posted November 17, 2004 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(); } } Quote
jdemackio Posted November 17, 2004 Posted November 17, 2004 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. Quote - The only true knowledge is knowing that you know nothing.
*Experts* Nerseus Posted November 17, 2004 *Experts* Posted November 17, 2004 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 Quote "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
Simcoder Posted November 17, 2004 Author Posted November 17, 2004 Thanks for the replys. Thats exactly what I needed Nerseus. Your the man =) -=Simcoder=- Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.