C# reading one word at a time from a text file

Started by
5 comments, last by Zahlman 16 years, 9 months ago
It seems like this should be really easy. It probably is, but I'm missing something. I want to read in a line from a text file, one word at a time. I'm using a StreamReader to read in from the file. From the MSDN documentation, it looks like it's pretty trivial to read in either a line at a time or a character at a time. Is there a way to read in one word at a time (white space separated words)? /me feels dumb. And tired.
Advertisement
Driv3MeFar,

I dont believe there is built in support to read 1 word at a time from a file. If someone knows of a way, please let us both know.

With that being said, I can think of two fundamental ways of obtaining the functionality you're looking for.

1. Peek 1 character at a time and check to see if the character is a space. If it is, everything up to that point is a word. Consume the space, discard it, and then start again. This can either be done by appending the character to the end of a string sequentially, or by peeking ahead of time and storing the start and end indices of words to pass to the Read method, and capture the entire word at once.

2. The second method is far simpler. Read the entire file into a string via a number of methods including StringReader.ReadToEnd or File.ReadAllText, etc... Once you've got a string containing the entire file, call String.Split with a space as the delimiter. This will return an array of strings, with each element in the array being a single word.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote:Original post by JWalsh
2. The second method is far simpler. Read the entire file into a string via a number of methods including StringReader.ReadToEnd or File.ReadAllText, etc... Once you've got a string containing the entire file, call String.Split with a space as the delimiter. This will return an array of strings, with each element in the array being a single word.


Yeah, this is the option I went with. It works very well and is certainly easy, but it isn't quite as clean as I would like. I guess I should take this as a not-so-subtle hint that I should be using xml for my file needs [grin].

Thanks for the help!
It depends on your situation... for simpler programs i would use method 1... for more complex programs method 2..
for #1 it would be something like this:
read from index 0 until '\n'
i dont know the syntax of the language or the read method but i'm sure its something like a file stream...
filestream = fopen(filename)

filestream.readline(blah)
if its not built in maybe there is something like readchar in your language then it would just be foreach(character as char) { if (char == '\n') { break;}
something like that
-durfy
The easy way is to read a line, and then use the String.Split method using a space and punctuations as the split characters to break the line up into words. Remember a space is just another character, nothing special to the computer.

theTroll
I struggled with exactly this. As stated above, I ended up manually using Peek() and Read() a bit like:

string NextWord(StreamReader s){    int c=s.Peek();    while(c!=-1 && Char.IsWhitespace(Convert.ToChar(c)))        {        s.Read(); c=s.Peek();        }    if(c==-1) return "";    StringBuilder b=new StringBuilder();    while(c!=-1 && !Char.IsWhitespace(Convert.ToChar(c))        {        b.Append(Convert.ToChar(c)); s.Read(); c=s.Peek();        }    return b.ToString();}


Or something. That was off the top of my head so probably won't work.

Very ugly, I know. Someone did suggest that you could use the regular expression stuff in .NET to make it easier, but I still think that required reading in large blocks of text at a time.
Quote:Original post by Driv3MeFar
I guess I should take this as a not-so-subtle hint that I should be representing my data as Python expressions (typically nested lists/dicts) which I can then just eval() [grin].


That's what I do, anyway. <3 Python [smile]

This topic is closed to new replies.

Advertisement