Sign in to follow this  
Driv3MeFar

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

Recommended Posts

Driv3MeFar    1080
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.

Share this post


Link to post
Share on other sites
JWalsh    498
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!

Share this post


Link to post
Share on other sites
Driv3MeFar    1080
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!

Share this post


Link to post
Share on other sites
Durfy    109
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

Share this post


Link to post
Share on other sites
TheTroll    883
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

Share this post


Link to post
Share on other sites
Aardvajk    13207
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.

Share this post


Link to post
Share on other sites
Zahlman    1682
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this