file.size()

How do you get the file size exactly

i see there is an instruction file.size(). and it is working in my sketch also. But is that an instruction which you must do, when the file is open on the SD card ? Or is it also possible after a close the file

i actually want to know the size of the file after all data is saved, and that is now when i close the file

Don't know the library you are using, but it probably has some examples in which file.size() is used.

my guess is that that it will work with any file open or close.

Did a test, en found out that it can only be done when the file is open, so than he knows which file has to be checked.. So what i do now, is when file is open , use flush, than check the size and than close it

file.size() is always correct for the open file. You don't need to do a sync, size is updated when you print or write to the file.

I added two statements to the ReadWrite.ino example.

    Serial.println();
    Serial.println(myFile.size());    
    myFile.println("testing 1, 2, 3.");
    Serial.println(myFile.size());

Here is output from the example:

Initializing SD card...initialization done.
Writing to test.txt...
0
18
done.
test.txt:
testing 1, 2, 3.

Before myFile.println("testing 1, 2, 3."), the size was zero and after size returned 18. The size of "testing 1, 2, 3.\r\n" is 18. println adds the CR/LF.

I used code

File         logFile;

logFile << year() << "-" << month() << "-" << day() << ";

in my case the data will only be writen to the file, when i write to it by flush or close it

the code

myFile.println("testing 1, 2, 3.");

so it writings the data directly

in my case the data will only be writen to the file, when i write to it by flush or close it

The value returned for file size is not dependent on flush.

logFile << year() << "-" << month() << "-" << day() << ";

File size will be updated several times while the above statement executes. I wrote SdFat so I know that file size is updates after each << insertion operator.

When the entire statement has finished, calling flush will not change the value returned by file size.

The value returned by file size after the file is closed may or may not be correct for the last file that was open.

OK, i thought that the data, which i saving to SD , would be first buffered. And by using flush or close it would be saved complete

Thanks for the update