Keeping a File Open?

Hello all,

Using a: SparkFun microSD Shield - DEV-12761 - SparkFun Electronics
With: Google Code Archive - Long-term storage for Google Code Project Hosting.

on an Uno Rev3.

I'm doing some data logging out to a file on the sd card fine but part of it is making me twitchy.

void loop()
{
  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
  ofstream sdout("test.csv", ios::out | ios::app);
  //Do logging to sdout here
  //eg sdout << "test" << flush
  sdout.close();
}

Is there anyway to keep the file handle open between loops? This works fine I'm just concerned that this sort of behaviour seems inefficient/detrimental to the sd card.

I've tried declaring some of the stuff in setup()/ the main file body but this does not work, the examples with the library seem to agree that this is the only way to write to a file within loop()

Thanks :slight_smile:

put the following in your void setup()

  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
  ofstream sdout("test.csv", ios::out | ios::app);

your just opening the same file over and over again

and no its not the only way, there's a write() function, post your full code

The SD::begin method should be called ONCE, not every pass through loop.

PaulS:
The SD::begin method should be called ONCE, not every pass through loop.

I was aware of this, my entire post details that i tried that.

Many thanks to the both of you!