3

How to start reading file from 2nd line skipping 1st line. This seems to work but is it best way to do so?

            using (StreamReader sr = new StreamReader(varFile, Encoding.GetEncoding(1250))) {
                string[] stringSeparator = new string[] { "\",\"" };
                int i = 0;
                while (!sr.EndOfStream) {                    
                    string line = sr.ReadLine(); //.Trim('"');
                    if (i > 0) {
                        string[] values = line.Split(stringSeparator, StringSplitOptions.None);

                        for (int index = 0; index < values.Length; index++) {

                            MessageBox.Show(values[index].Trim('"'));
                        }
                    }
                    i++;
                }
            }
MadBoy
  • 10,824
  • 24
  • 95
  • 156

3 Answers3

13

If the file is not very large and can fit in memory:

foreach (var line in File.ReadAllLines(varFile, Encoding.GetEncoding(1250)).Skip(1))
{
    string[] values = line.Split(',');
    ...
}

If not write an iterator:

public IEnumerable<string> ReadAllLines(string filename, Encoding encoding)
{
    using (var reader = new StreamReader(filename, encoding))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line;
        }
    }
}

and then consume it:

foreach (var line in ReadAllLines(varFile, Encoding.GetEncoding(1250)).Skip(1))
{
    string[] values = line.Split(',');
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
6

Could you not just read the first line outside of the loop without assigning it to a variable?

using (StreamReader sr = new StreamReader(varFile, Encoding.GetEncoding(1250))) {
            string[] stringSeparator = new string[] { "\",\"" };
            if (!sr.EndOfStream)
                sr.ReadLine();
            while (!sr.EndOfStream) {                    
                string line = sr.ReadLine(); //.Trim('"');
                string[] values = line.Split(stringSeparator, StringSplitOptions.None);

                for (int index = 0; index < values.Length; index++) {
                    MessageBox.Show(values[index].Trim('"'));
                }
            }
        }
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • It seems to work fine, but I'll go with Darin option. Seems much "cleaner" way to do so :-) – MadBoy Apr 11 '11 at 08:48
1

I'm sorry but I see no problem with the way you are doing it though. I couldn't add comment.

So just for the sake of answering, you probably could have try to call ReadLine() once before the loop. Might not be the best way as I don't know whats the behavior of running ReadLine() if its already end of stream, but it nothing is gonna happen then thats gonna save you some checks.

Updated:

To give a more complete answer, calling ReadLine() when the stream is at its end will return a null.

Reference: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

Remember to check the return for null value.

lxcid
  • 1,023
  • 1
  • 11
  • 18
  • 2
    Why are you answering if you do not know? Guessing do not help anyone. – jgauffin Apr 11 '11 at 08:45
  • I answered because I want to let him know that I think his way is pretty decent, which is what he ask about. Yes skip() is a great way of doing it, more intuitive and readable but I don't see how the underlying code of skip won't be using some read line or char search and transverse until the specific line has skipped. I am not guessing, I just comment based on what I feel like. Look if I have ability to comment I would have done it. That's why I mentioned I couldn't comment. – lxcid Apr 11 '11 at 09:04
  • I was refering to `I don't know whats the behavior of running ReadLine() if its already end of stream`. – jgauffin Apr 11 '11 at 09:11
  • I changed the -1 vote to +1. You should always know what you are talking about if you want to answer questions. Guessing in answers makes them just become noise and doesn't help. – jgauffin Apr 11 '11 at 09:24