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

File.ReadLines() on .NETMF

File IO

  • Please log in to reply
2 replies to this topic

#1 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 09 May 2013 - 05:54 PM

Hi all,

 

I would like to modify my project to include a method to read the most recent entry from a datafile, however, I can't afford to load the whole file into memory, for by the end of the day it's several hundred KB in size.

 

In regular .NET, I could say such as

string[] gooby = File.ReadLines(dataFile).Last().Split(',');

And be done with it. However, .NETMF has no ReadLines() method. Ideas?



#2 martin2250

martin2250

    Advanced Member

  • Members
  • PipPipPip
  • 37 posts
  • LocationGermany

Posted 09 May 2013 - 09:47 PM

Hi Verdris,

 

first of all, even if that method was implemented, it wouldn't work because it would read all lines to RAM and then pick the last one, that would result in an OutOfMemoryException.

 

My approach would be a making a new StreamReader, and then work with the position value. If you want to read the last line, you should save its position to the first 4 bytes of the file everytime you're appending something to the file, then later read them and set the position of the StreamReader accordingly.

 

Greetings,

 

Martin



#3 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 09 May 2013 - 11:06 PM

Hi Verdris,

 

first of all, even if that method was implemented, it wouldn't work because it would read all lines to RAM and then pick the last one, that would result in an OutOfMemoryException.

 

My approach would be a making a new StreamReader, and then work with the position value. If you want to read the last line, you should save its position to the first 4 bytes of the file everytime you're appending something to the file, then later read them and set the position of the StreamReader accordingly.

 

Greetings,

 

Martin

It was my understanding that ReadLines() is a streaming method. Thanks for clarifying!







Also tagged with one or more of these keywords: File IO

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.