• Welcome to Valhalla Legends Archive.
 

C#.Net Reading Text Files

Started by Dale, June 27, 2007, 01:05 PM

Previous topic - Next topic

Dale

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!

shout

#1

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;
}


I didn't test this but it should look something like this.

Dale

Thanks, But since that shit totally confuses me, I get an "Unreachable Code Detected" and return s; is underlined, how do I fix this?

shout

#3
Err...

put

break;

after sr.Close();

And what don't you understand about it? I'll do my best to explain.

iNsaNe

use System.IO.File, you will probably want System.IO.File.ReadAllText or ReadAllLines. There is also a function to ReadAllBytes.

MyndFyre

Quote from: iNsaNe on September 28, 2007, 10:32 PM
use System.IO.File, you will probably want System.IO.File.ReadAllText or ReadAllLines. There is also a function to ReadAllBytes.
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Dale

I could of just read the whole file, and put it into an array..