Pages: [1]   Go Down
Author Topic: Keeping a File Open?  (Read 1221 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

Using a: https://www.sparkfun.com/products/9802
With: http://code.google.com/p/sdfatlib/

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.
Code:
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 smiley

« Last Edit: October 14, 2012, 09:02:46 pm by ahref » Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

put the following in your void setup()

Code:
 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
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1]   Go Up
Jump to: