StreamReader detect end of file - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

StreamReader detect end of file


  • Please log in to reply
3 replies to this topic

#1 moodcutter

moodcutter

    New Member

  • Members
  • Pip
  • 5 posts

Posted 19 May 2011 - 03:51 AM

Hi,

Received the Netduino Plus (f/w 4.1.0.6) last week and am getting to know it.

I'm having trouble detecting the end of a file when using StreamReader. The code below will output the contents of the file line by line, but it doesn't seem to detect the file end. The final Debug.Print statement after the do-while loop is not executed.

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace SDRead
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            string fileLine = "";
            Directory.SetCurrentDirectory("\\SD");
            StreamReader fileReader = new StreamReader("test.txt");
            do
            {
                fileLine = fileReader.ReadLine();
                Debug.Print(fileLine);
            }
            while (fileReader.Peek() != -1);

            fileReader.Close();
            Debug.Print("End file read");
        }

    }
}

Any suggestions will be appreciated.

Cheers

#2 moodcutter

moodcutter

    New Member

  • Members
  • Pip
  • 5 posts

Posted 19 May 2011 - 10:27 PM

I don't know why the "fileReader.Peek() != -1" does not work (it is .NET MF method), but "fileReader.EndOfStream == false" works as an alternative.

#3 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 12 July 2012 - 05:32 AM

Still the same until today with the .NET Micro Framework 4.2 (RC5).

This does the trick:


do
{
var line = sr.ReadLine().Split(' ');
}
while (!sr.EndOfStream);


Edited by Giuliano, 12 July 2012 - 05:33 AM.


#4 Nobby

Nobby

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 18 July 2012 - 11:07 AM

another nice trick is the following

string fileLine = "";
while((line=sr.ReadLine())!=null)
{
     Debug.Print(fileLine);
}
sr.Close();





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.