Go Down

Topic: Why can't I declare a File as a global? (Read 1 time) previous topic - next topic

__Tango

In the "DataLogger" Yun example sketch, the File object is declared as a local variable, and the file is consistently opened and closed in each loop:
Code: [Select]

void loop() {
...

  File dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
...
}


I was trying to code something that set the File object as a global and write to it throughout the entire lifetime of the sketch (using flush()) to sync the data to disk, like this:

Code: [Select]

#include <FileIO.h>

File dataFile;
void setup() {
  // Initialize the Bridge and the Serial
  Bridge.begin();
  Serial.begin(115200);
  FileSystem.begin();

  while (!Serial); // wait for Serial port to connect.
  Serial.println("Filesystem datalogger\n");
}

void loop() {
  dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("hello");
    dataFile.close();
  }
  else {
    Serial.println("error opening datalog.txt");
  }
  delay(15000);
}


However, I get a compile time error
Code: [Select]

/Users/tango/src/Arduino/YunFileTest/YunFileTest.ino: In function 'void loop()':
YunFileTest:15: error: use of deleted function 'BridgeLib::File& BridgeLib::File::operator=(const BridgeLib::File&)'
   dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND);
            ^
In file included from /Users/tango/src/Arduino/YunFileTest/YunFileTest.ino:1:0:
/Applications/Arduino-1.6.6-copy.app/Contents/Java/libraries/Bridge/src/FileIO.h:30:7: note: 'BridgeLib::File& BridgeLib::File::operator=(const BridgeLib::File&)' is implicitly deleted because the default definition would be ill-formed:
 class File : public Stream {
       ^
/Applications/Arduino-1.6.6-copy.app/Contents/Java/libraries/Bridge/src/FileIO.h:30:7: error: non-static reference member 'BridgeClass& BridgeLib::File::bridge', can't use default assignment operator
exit status 1
use of deleted function 'BridgeLib::File& BridgeLib::File::operator=(const BridgeLib::File&)'


Why can you use a File object as a local variable, but not a global?

ShapeShifter

I'm not sure why it isn't letting you do that, I'm still trying to figure out what the File class code is doing.

But, if your goal is to not keep opening/closing the file, your code isn't doing that. At the beginning of loop(), you are calling FileSystem.open, which is creating a new File object and assigning it to your global variable. So you are still creating a new File object each time, but in this case you are not closing the file. The ~File destructor would automatically call close() for you at the end of loop() if the file object were local, but since it is global and you are just overwriting the old object with the new object, you are not necessarily going to call the close() function automatically, and you could eventually run out of resources. (For example, if you keep creating YunClient objects, and don't close() them, you will eventually stop accepting new connections after 255 connections, there could be a similar issue with the File objects.)

If you can get the global File variable to work, you need to be more careful with your File object management. Only open it if you are sure it is not already open, and be sure to close() the previous instance if you are about to overwrite it with a new file open call.

sonnyyu

#2
Nov 11, 2015, 02:50 pm Last Edit: Nov 11, 2015, 03:26 pm by sonnyyu
It is a issue of FileIO File class lacks default assignment operator

https://github.com/arduino/Arduino/issues/1810

Sample work around:Rewrite FileIO  for your application (read  one line at time).

http://forum.arduino.cc/index.php?topic=267376.0





__Tango

#3
Nov 11, 2015, 11:22 pm Last Edit: Nov 11, 2015, 11:23 pm by __Tango
It is a issue of FileIO File class lacks default assignment operator

https://github.com/arduino/Arduino/issues/1810
Great. I hope it gets fixed.

Sample work around:Rewrite FileIO  for your application (read  one line at time).

http://forum.arduino.cc/index.php?topic=267376.0
I'm trying to write a file one line at a time (not read). 



[/quote]

sonnyyu

#4
Nov 14, 2015, 02:09 pm Last Edit: Nov 14, 2015, 02:09 pm by sonnyyu
Great. I hope it gets fixed.
...
Not yet, but if anyone fix it could send pull request.

...
I'm trying to write a file one line at a time (not read).  
That is why I wrote "Sample".



Go Up