How to change text in text file on SD card

Hey all !

With the help of a GPS shield I want to log daily kilometers on an SD card (therefor in a text file).

I would like to write the total distance everytime I get a GPS position (then the algorythm would make the seperation by day)

For example : I receive a GPS position, I calculate the distance to the previous position, I check fromthe GPS if today is the same day as the one written on the SD card if yes : add this one to the total distance, write/update the result on the SD card. if no : create a new line with today's date, add this one to the total distance, write/update the result on the SD card. loop again.

Would you have any idea how I can do this ? I didn't find a thread where I can find my answer ... could you help me with some keyword to search for ?

Thank you for your help !

You'd need to open the file for read/write. Read until you find a carriage return (end of record marker). Each time you find one, record the position in the file. If the current record is for today, reposition to the previous carriage return, advance one character, and write the new data.

This implies that all records are the same size, so a new record is not longer than the record that was there. If today's record is always the last record, this does not apply.

It might be easier, though, to just add a new record, at the end, with the new GPS data and today's date, and do the processing when you use the SD card data elsewhere.

PaulS, thanks for your repply and advice.

I thought keeping the result in the SD card prevent data loss. meaning if I keep today's kilometer in a variable (in the algorythm), if the power is lost, then today's mileage is lost as well. ... since I'll power the arduino with power from the car (through a 12V to USB power adaptor), I want to make sure that when the car is powered down, the the mileage has already been recorded and updated.

Or maybe you think of another solution ? (I haven't had much time to practice on arduino-DIY-electronic-coding ... so please excuse my possible misunderstanding/lack of knowledge).

I thought keeping the result in the SD card prevent data loss.

It would.

meaning if I keep today's kilometer in a variable (in the algorythm), if the power is lost, then today's mileage is lost as well.

True.

Whether the data is accumulated in a single row, or there are several rows per day, is really important only when that SD card is read again. You haven't defined why you are doing this, or what value the data has (or where it will be used), so the only advice you will get is going to be very generic.

PaulS: You haven't defined why you are doing this, or what value the data has (or where it will be used), so the only advice you will get is going to be very generic.

true. I didn't want my first explenation message to be too long, that's why. what I would like to build is a kilometers/mileage logger and end up with a text file of this form : "The 01/11/2011 you drove 56 kilometers" Second step of my mini-project : I'll put a switch on my kit and the file would look like : "The 01/11/2011 you drove 42 km for your work and 14 for anything else"

I would have this line in the file for any day that I drive. At the end of the month or year, I can retreive the data and use it on excel, for example.

Have you seen similar use or example on the forum that I wouldn't have seen through my searches ?

At the end of the month or year, I can retreive the data and use it on excel, for example.

I think that it would be much simpler to write to the SD card something like w: date distance o: date distance

Then, create an application on the PC that would read all the entries in the file, and add up the distances for the appropriate date, for the w: records (work) and the o: records (non-work).

IIRC, the data is intended to be used to get tax credits for miles driven for employment purposes, presumably in a personal vehicle.

How about this for storing the data on the SD card: use two csv (or other separator) files so they’re easy to pull into Excel. One is scratch, holding todays data - overwrite it with a single line every time things change containing the date,work miles,other miles. The other file is history. Same format, one line per day. In setup, get the date from the gps, read scratch. If the dates are the same, we had a power outage - store the mileage data in arduino variables. If it’s a new day, append the data from scratch to the history file, zero your variables, overwrite scratch with newdate,0,0.

So, scratch accumulates the days data, so you can survive loss of power, then pushes it to history next day for your tax records. Working past midnight would need a little additional code.

You probably want to use some temp files too so that you don’t lose data if the power is lost while you’re performing SD operations.

wildbill:
IIRC, the data is intended to be used to get tax credits for miles driven for employment purposes, presumably in a personal vehicle.

How about this for storing the data on the SD card: use two csv (or other separator) files so they’re easy to pull into Excel. One is scratch, holding todays data - overwrite it with a single line every time things change containing the date,work miles,other miles. The other file is history. Same format, one line per day. In setup, get the date from the gps, read scratch. If the dates are the same, we had a power outage - store the mileage data in arduino variables. If it’s a new day, append the data from scratch to the history file, zero your variables, overwrite scratch with newdate,0,0.

So, scratch accumulates the days data, so you can survive loss of power, then pushes it to history next day for your tax records. Working past midnight would need a little additional code.

You probably want to use some temp files too so that you don’t lose data if the power is lost while you’re performing SD operations.

This is the impression I get. My dad uses his personal vehicle for field service work, 2005 Ram 1500 hemi with 268,000 miles on it. But he gets paid by the mile by his work. I’m guessing this project is for self employment.