Valhalla Legends Forums Archive | .NET Platform | C#.Net Reading Text Files

AuthorMessageTime
dlStevens
How would I go about, reading every some lines, in a text file?

I know how to read from a file, and that works, I just can't seem to get it to read say, every 7 lines.

Any help would be appreciated!
June 27, 2007, 6:05 PM
shout
[code]
public static string[] ReadLines(string file, int skip)
{
string[] s = new string[0];
StreamReader sr = new StreamReader(file);
for (int i = 0; ;i++)
{
try
{
string st = sr.ReadLine();
if (i % skip == 0)
{
string[] sn = new string[s.Length + 1];
Array.Copy(s, 0, sn, 0, s.Length);
sn[sn.Length - 1] = st;
s = sn;
}
}
catch (System.IO.IOException e)
{
sr.Close();
}
}
return s;
}
[/code]

I didn't test this but it should look something like this.
June 27, 2007, 6:54 PM
dlStevens
Thanks, But since that shit totally confuses me, I get an "Unreachable Code Detected" and return s; is underlined, how do I fix this?
June 27, 2007, 7:08 PM
shout
Err...

put
[code]
break;
[/code]
after sr.Close();

And what don't you understand about it? I'll do my best to explain.
June 27, 2007, 8:58 PM
iNsaNe
use System.IO.File, you will probably want System.IO.File.ReadAllText or ReadAllLines. There is also a function to ReadAllBytes.
September 29, 2007, 3:32 AM
Myndfyr
[quote author=iNsaNe link=topic=16826.msg173372#msg173372 date=1191036748]
use System.IO.File, you will probably want System.IO.File.ReadAllText or ReadAllLines. There is also a function to ReadAllBytes.
[/quote]
Couple thoughts....

First, this thread had been dead for a while....

The second, is that using StreamReader.ReadLine() is probably more effective to code in this case, because the StreamReader object handles parsing each line.  On the other hand, if you use File.ReadAllText(), you need to split the string out.  I suspect that ReadLine() makes it a bit clearer in intent as well.
September 30, 2007, 6:26 AM
dlStevens
I could of just read the whole file, and put it into an array..
September 30, 2007, 4:31 PM

Search