943,925 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 839
  • C# RSS
Jul 17th, 2009
0

File input method returning blank lines

Expand Post »
As far as I can tell, this method is returning a blank line every other line, but I can't figure out why:
C# Syntax (Toggle Plain Text)
  1. public static List<string> plain(string path, int num)
  2. {
  3. List<string> result = new List<string>();
  4. StreamReader reader = File.OpenText(path);
  5. string line;
  6. int i = 0;
  7. while ((line = reader.ReadLine()) != null)
  8. {
  9. result.Add(line);
  10. i++;
  11. if (i == num) { break; }
  12. }
  13. return result;
  14. }

Does anyone have an idea? I'm sure it's something simple I'm overlooking .
Last edited by tdeck; Jul 17th, 2009 at 3:07 pm.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Jul 17th, 2009
1

Re: File input method returning blank lines

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.
Reputation Points: 72
Solved Threads: 15
Junior Poster in Training
lighthead is offline Offline
64 posts
since Jan 2008
Jul 17th, 2009
1

Re: File input method returning blank lines

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 )?
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Jul 17th, 2009
0

Re: File input method returning blank lines

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/pythonfo...=13807&start=0

-Weird
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Jul 17th, 2009
0

Re: File input method returning blank lines

In that case you might want to use this:
c# Syntax (Toggle Plain Text)
  1. line.Replace("\\r\\r\\n", "");

That should fix it :-)
Reputation Points: 10
Solved Threads: 8
Junior Poster
sid78669 is offline Offline
195 posts
since Nov 2008
Jul 17th, 2009
0

Re: File input method returning blank lines

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 .
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Jul 17th, 2009
0

Re: File input method returning blank lines

Mark it as solved.
Reputation Points: 72
Solved Threads: 15
Junior Poster in Training
lighthead is offline Offline
64 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Using List and Random
Next Thread in C# Forum Timeline: windows control library





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC