As far as I can tell, this method is returning a blank line every other line, but I can't figure out why:

public static List<string> plain(string path, int num)
        {
            List<string> result = new List<string>();
            StreamReader reader = File.OpenText(path);
            string line;
            int i = 0;
            while ((line = reader.ReadLine()) != null)
            {
                result.Add(line);
                i++;
                if (i == num) { break; }
            }
            return result;
        }

Does anyone have an idea? I'm sure it's something simple I'm overlooking :).

Recommended Answers

All 6 Replies

Your function is correctly returning a list of at most 'num' lines in the file. There were no blank lines. You might want to recheck other parts of the program.

Yeah .. it also works fine here.

P.S. Are you sure, that your file, which lays in path has text in it (maybe this is that simple, that you're overlooking :P)?

Hmm... I think it's the input file I used. It's generated from a python script, and I think the script replaced the \n with \r\n. So, since I'm sending \r\n, it puts \r\r\n in the file. Which, oddly, shows up as just one line break in notepad and textpad :).

In case you're curious:
http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=13807&start=0

-Weird

In that case you might want to use this:

line.Replace("\\r\\r\\n", "");

That should fix it :-)

Yeah I just wrote a Python script to fix the files. Python's pretty quick to write, this is the first time it has frustrated me :).

Mark it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.