Pages: [1]   Go Down
Author Topic: logFile.close  (Read 1145 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

i have a question about using a file on you SD cart

normally you open a file SD.open.....  than write some data in it , and when you done close the file logFile.close()
But in my program it is opened at 00:00h and closed at 23:55h, 

If i reset or powerdown between that time, the arduino will  NOT  give a logFile.close() .
Could the file then be corrupted or get corrupted, or ??? of will the pointer be lost , where file is ended (last written character)

i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file..  is there some other program closing the file , or is thr SD cart protected against this

if a file could get corrupted , if there a check for this

geetings
 

Logged

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

Quote
But in my program it is opened at 00:00h and closed at 23:55h, 
Why? It should be opened when you need to write to it, and closed when you are done writing.

Quote
If i reset or powerdown between that time, the arduino will  NOT  give a logFile.close() .
That's not good.

Quote
Could the file then be corrupted or get corrupted
Yes.

Quote
i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file..  is there some other program closing the file , or is thr SD cart protected against this
You've been lucky. I don't advise continuing a bad practice, though.

Quote
if a file could get corrupted , if there a check for this
Sure. Define corrupted. Detect where the files is corrupted, and write an EOF in that location. The file will no longer be corrupted.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But in my program it is opened at 00:00h and closed at 23:55h, 
Why? It should be opened when you need to write to it, and closed when you are done writing.

Quote
If i reset or powerdown between that time, the arduino will  NOT  give a logFile.close() .
That's not good.

Quote
Could the file then be corrupted or get corrupted
Yes.

Quote
i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file..  is there some other program closing the file , or is thr SD cart protected against this
You've been lucky. I don't advise continuing a bad practice, though.

Quote
if a file could get corrupted , if there a check for this
Sure. Define corrupted. Detect where the files is corrupted, and write an EOF in that location. The file will no longer be corrupted.

i write every 5 minutes data to it, so thats the reason i do not close it every time
Logged

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

Quote
i write every 5 minutes data to it, so thats the reason i do not close it every time
That's not a reason. That's an excuse.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i write every 5 minutes data to it, so thats the reason i do not close it every time
That's not a reason. That's an excuse.

its the easiers way..  to leave it open

So your advice  open file write data and close it .. next 5 min open it again etc..  OK ., so window for crashing file is very small(only during writing)
But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?


it see on the arduino.cc page
http://arduino.cc/en/Reference/SDCardNotes

Opening/Closing files
When you use file.write(), it doesn't write to the card until you flush() or close(). Whenever you open a file, be sure to close it to save your data.

So a flush will save it directly , what will do en close instruction , save an EOF???

 
« Last Edit: February 25, 2013, 03:51:40 pm by logger » Logged

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

Quote
its the easiers way..  to leave it open
Sure, but it's not the right way.

Quote
So your advice  open file write data and close it .. next 5 min open it again etc..  OK ., so window for crashing file is very small(only during writing)
Yes, it is.

Quote
But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?
Depends on how you open the file. If you open in append mode, then, yes, the write pointer is positioned at the end of the file.

Quote
So a flush will save it directly , what will do en close instruction , save an EOF???
Each time you call the write() (or print() or println()) method, data is written to a buffer. The flush() method actually causes a write to the file, copying the buffer contents into the file. The close() method calls flush(). Closing the file does write an EOF into the file, while flush() does not.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
its the easiers way..  to leave it open
Sure, but it's not the right way.

Quote
So your advice  open file write data and close it .. next 5 min open it again etc..  OK ., so window for crashing file is very small(only during writing)
Yes, it is.

Quote
But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?
Depends on how you open the file. If you open in append mode, then, yes, the write pointer is positioned at the end of the file.

Quote
So a flush will save it directly , what will do en close instruction , save an EOF???
Each time you call the write() (or print() or println()) method, data is written to a buffer. The flush() method actually causes a write to the file, copying the buffer contents into the file. The close() method calls flush(). Closing the file does write an EOF into the file, while flush() does not.

OK thx for information 

so only difference is the EOF , in my case . This could course a corrupt file , because it is not properly closed 

file corrupt dectetion
can i detect it by  . if file SD.exist  then , try SD.open

a File object referring to the opened file; if the file couldn't be opened, this object will evaluate to false in a boolean context, i.e. you can test the return value with "if (f)".

Logged

Pages: [1]   Go Up
Jump to: