Go Down

Topic: logFile.close (Read 1 time) previous topic - next topic

logger

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



PaulS

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.

logger


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

PaulS

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.

logger

#4
Feb 25, 2013, 09:40 pm Last Edit: Feb 25, 2013, 09:51 pm by logger Reason: 1

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???


PaulS

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.

logger


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)".


Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview