Go Down

Topic: number of lines in a file on SD card (Read 8616 times) previous topic - next topic

logger

Hi

Im logging evert 5 minutes my data in a file. Every day i make a new file.. But for some reason  sometimes i missing some loggings

So i want to check if the loggings are written on the SD card , by checking the number of lines in  the file

IS there a simple way or a function , how to check this... and not by reading line by line withe a counter?

I found the function "file.position()"  but how does it work??

greetings

marco_c

You need to read line by line, or character by character, to work out the number of lines.

A simpler way to do this in your circumstances may be to have a second file with the number of lines in the file. Every time you write a bunch of lines in the data file, you read the 'line' file, add the number you have just written and rewrite the value out to the line file.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

robtillaart

if all the lines have the same size you could divide the file size by the (fixed) line size.
This is much faster but only works for fixed line sizes.

(do not forget to count the CRLF)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

logger

hi

Mmmhhh an extra file ,,  adding a extra number , thats an idee, indeed. But it must also be suitable, when my software is completely resetted. All counters are resetted (only info in eeprom is still present). Writing every 5 min to an eeprom is not a good idee to me

reading line for line , than looks the best way. Or i have to save a counter in a file, and every time i have a new logging, i have to read the counter from the file and increase it ans save it back..

but there should be a pionter in a file , so every time it opens the file it knows where it was last time...  i thought the     
"file.position()" function should do that . without reading all the data in the file , it reads only the position




im reading it

robtillaart

Quote
So i want to check if the loggings are written on the SD card , by checking the number of lines in  the file

IS there a simple way or a function , how to check this..

back to the original problem - you could check the file size before and  after the write...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

logger

The file size before logging will be a probleem, when software has resetted. Then  i do not know the size anymore

PaulS

Quote
The file size before logging will be a probleem, when software has resetted. Then  i do not know the size anymore

Reading the size of a file before you write to it and after you write to it is exactly the same process, It has nothing to do with the Arduino be reset or the software being reset (whatever that means).

Open the file for write. Read the position of the write cursor. Close the file. You know how big the file is. Open the file for write. Write some data. Read the position of the write cursor. Close the file Now, you know how big the file is after adding to it. The change in size is the number of characters added to the file.
The art of getting good answers lies in asking good questions.

logger

#7
Jan 19, 2014, 07:02 pm Last Edit: Jan 19, 2014, 07:08 pm by logger Reason: 1
The reset, could be generated, bY watchdog or powerfailer.
The arduino is restarting after that, so no values are known

If i open the file, it would be  nice bY asking position, how many lines are written(lines do not have to samen size).
By reading, file.position() want info i get?
I want to know, after a quick scan, how many lines are written, so i get a overview how many lines/loggings are missed.
Not only if last logging was written

PaulS

Quote
The arduino is restarting after that, so no values are known

Learn them. The size of the file is not affected by resetting the Arduino, unless you are failing to close the file correctly. Determining the size of a file before writing to it is trivial. Learning the size of the file after writing to it is equally trivial.

Quote
If i open the file, it would be  nice bY asking position, how many lines are written(lines do not have to samen size).

If your records are not all the same length, then the size of the file is no indication of the number of records. You must then open the file, read and count each record, and close the file.

Quote
By reading, file.position() want info i get?

The location where you will read or write, depending on the mode of the file. A file opened for write has the write point positioned at the end of the file, so file.position will tell you the number of bytes in the file.

Quote
Not only if bedt logging wad written

Good thing, as I have no idea what this means.
The art of getting good answers lies in asking good questions.

robtillaart


Quote
By reading, file.position() want info i get?

The location where you will read or write, depending on the mode of the file. A file opened for write has the write point positioned at the end of the file, so file.position will tell you the number of bytes in the file.

think you mean append mode?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulS

Quote
think you mean append mode?

If the file is to be truncated (the only other option), that happens when the file is closed. So, position does tell you (correctly) the number of bytes in the file when it is (to be) closed.

But, yes, I should have made it clear that append and truncate have an impact on the value in position.
The art of getting good answers lies in asking good questions.

logger

OK, so file.position() returns the bytes of the file at that position (read or write postions)  so it is actually the size what is returned , not the position..  

this function is than not usefull in my case,  
by the way , some text was not written correctly  on this post, my phone was correcting  my line in a wrong text
sorry for that..



logger

you mean O_APPEND - If set, the file offset shall be set to the end of the file prior to each write
i use for writing
logFile = SD.open(fullName, O_CREAT | O_APPEND | O_WRITE);..  but how can i use it for reading number of lines then?

PaulS

Quote
but how can i use it for reading number of lines then?

Fixed length records: Divide the position value by the length of each record.

Variable length records: You can't. Get over it.
The art of getting good answers lies in asking good questions.

logger

OK thanx for info

i will check the size of the file before and after next saving in normal situation every 5 min(So i know a new line was saved)
And when the arduino has restarted for some reason , i will count line by line the file

i think thats the best way

greetings, thanx for help 

Go Up