Is there any way to read strings from a file

  • Thread starter Thread starter H
  • Start date Start date
H

H

Now, I'm here with another newbie question ....

I want to read a text file, string by string (to do some things with some
words etc etc), but I can't seem to find a way to do this String by String.
Is there anyway, like String s = something.ReadString() ?

Or what may be a fine way to do this ? Only thing I can some up with is to
read 1 char at a time, and look if the next char is a space-sign, and that
way "make" the Strings myself. but this isn't exactly an efficient way. The
other way I can think of is to read from the file, line by line into a
String, and then if there is some smart way to distinguish separate words in
a string, and then do what should be done on them that way. but this way is
also very clumsy, and syupid if there are lots and ots of Strings that
should be tampered with.
So, whats a clever way of doing this ?

TIA
 
The class you're after is the StreamReader. This provides a way of
extracting properly decoding the encoding (?) of the binary data stored in
the file. This gives you a "ReadLine()" method or a "ReadToEnd()" method,
which sounds like the thing you need, assuming the data in your file is
formatted in a "one item per line" manner.

Would strongly recommend that you move to XML if you happen to own the
format of the file.
 
Matthew Reynolds said:
The class you're after is the StreamReader. This provides a way of
extracting properly decoding the encoding (?) of the binary data stored in
the file. This gives you a "ReadLine()" method or a "ReadToEnd()" method,
which sounds like the thing you need, assuming the data in your file is
formatted in a "one item per line" manner.


Well, ReadLine and ReadToEnd doesn't really help me. Say for example that I
want to sort all the words in a text document (my app will only be handling
textfiles), then ReadLine would only give me a string (called myString) with
lots of words, and then I would have to work on myString to find out what
all the words in that string are etc etc. That makes a lot of work, and
makes the whole app really slow. But if that is the only way I guess I'd
have to do it that way ...
 
If you can fit whole file in available memory possibly easiest way would be
to use Regex (see Regular expressions in MSDN) on one big string, which you
can get with ReadToEnd method Regex has pretty good parsing capabilities
Including parsing for end-of-string delimiters and separate words.

If you'll want to optimize further reading-parsing loop, you can consider to
use asynchronous IO for reading file, intermediary object for buffer parsing
(e.g. creating strings or blocks of them) and multithreading for multiple
strings or blocks of strings parsing.

HTH
Alex
 
H said:
Well, ReadLine and ReadToEnd doesn't really help me. Say for example that I
want to sort all the words in a text document (my app will only be handling
textfiles), then ReadLine would only give me a string (called myString) with
lots of words, and then I would have to work on myString to find out what
all the words in that string are etc etc. That makes a lot of work, and
makes the whole app really slow. But if that is the only way I guess I'd
have to do it that way ...

Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.
 
Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.
to the group, please do not mail me too

Yep thats a way too, but I still have to do things on the larger strings,
which takes time.

Anyway, Thanks for all the answers !
 
H said:
to the group, please do not mail me too

Yep thats a way too, but I still have to do things on the larger strings,
which takes time.

So do it on individual lines... is the performance really unacceptable
when doing it on a line-by-line basis?
 
Jon Skeet said:
So do it on individual lines... is the performance really unacceptable
when doing it on a line-by-line basis?

No, I guess not, only it becomes clumsy, and I keep getting errors I don't
know why they appear ...
 
H said:
No, I guess not, only it becomes clumsy, and I keep getting errors I don't
know why they appear ...

Then I'd concentrate on finding why you get those errors before
abandoning the approach completely. If you've got a short but complete
sample program which demonstrates the problem, I'd be happy to look at
it.
 
Back
Top