I’m trying to do something very simple but it’s not working. I have a small text file on the SD card that I read and print but the code hangs in the peek function when the end of file is reached. Any idea what I’m doing wrong? Should I be doing this some other way?
using System;
using System.Threading;
using System.IO;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.Hardware;
namespace FPII_StreamReader
{
public class Program
{
public static void Main()
{
const string path = @ "\SD\weather.txt";
PersistentStorage sdPS;
// *** Turn off garbage collection output ***
Debug.EnableGCMessages(false);
// Blink board LED
bool ledState = false;
OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, ledState);
bool sdExists = PersistentStorage.DetectSDCard();
if (sdExists)
{
// Create a new storage device
sdPS = new PersistentStorage("SD");
// Mount the file system
sdPS.MountFileSystem();
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Debug.Print(sr.ReadLine());
}
}
}
sdPS.UnmountFileSystem();
sdPS.Dispose();
}
while (true)
{
// Sleep for 500 milliseconds
Thread.Sleep(500);
// toggle LED state
ledState = !ledState;
led.Write(ledState);
}
}
}
}