Zephyr . via .NET 247 wrote:
(Type your message here)
--------------------------------
From: Zephyr .
hey, i got trouble getting integers out of a plain text file.
i dont want to use binary files, just plain text files so they
can be hand-edited more easily.
now, for all i know (still a rookie)
there's StreamReader.Read() and StreamReader.ReadLine()
ReadLine() gets me somewhere, as it gets full string lines,
but as i need access to the integers, i have to use the Read()
which gives me strange?!? results. could anyone help me?
many thx
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>M6EI6Vs7ckGzsA2ft89Bnw==</Id>
Hey,
StreamReader.Read() will only return a single character at a time,
whereas StreamReader.ReadLine() will read an entire line from a file. I
assume these are the results you're seeing.
To better answer your question, it'd helpful to know the format of the
text file you're importing. If it's simply something like:
2134
253
364
35865
967
<etc...>
Then you can do something along the lines of:
while (reader.Peek() > -1)
{
int i = int.Parse(reader.ReadLine());
}
If the format is more complex, like:
123:234:235:346
Then you'd need to split the string you read in into an array, and
repeat the above, like so:
while (reader.Peek() > -1)
{
string[] numbers = reader.ReadLine().Split(":");
foreach (string s in numbers)
{
int i = int.Parse(s);
DoSomethingWithInteger(i);
}
}
Hopefully that helps out,
Clint