SD card writing to a specific line

Hi.

I'm currently programing a regulator, that reads settings from an SD card. Since opening a file and reading is not hard to accomplish. I am having some questions about writing to it. I would like to know if there is a way to open a file and edit a line in it. Let's say I want to open line 23 and edit a number 1234 to 4321. How can this be done?

I was thinking about large buffer that reads the whole file and then edits desired line, deletes the original file and writes a new one. Can this be done more efficient?

I googled this but found nothing.

Best regards

You could do that, if the file wasn't too large to fit into memory.

A more usual approach is to open a temporary file, copy line by line from the original to the new file, amending as required. Then delete the old file and rename the new one.

I will be referring to SD Card library on the Arduino site: http://arduino.cc/en/Reference/SD/

One option is to use the seek() function and determine what character line 23 is at. You would have to do this manually.

Another more elegant option is to use this:

/* SD_readLine()
*  Description: Captures description lines in files. Does not save the data.
*  Edited: 07/06/12
*/
File dataFile;
void SD_readLine()
{
  while ( dataFile.read() != '\n' );  // Read the entire line but do not save it.
}

This will read the entire line. You can run this 23 times to get to the beginning of line 23. Now what you would like is difficult since I do not think data storage devices are meant for "live" data manipulation. When you open a file on your computer from your SD card, you are not doing live data manipulation. You are actually making changes and then saving it. The computer is storing all that data in ram (or something of the sort).

You can use this general concept and store everything in the Aduino ram but be aware the SD library takes ALOT of ram! I have had major issues with my entire Arduino resetting due to the SD library taking up the ram. Thus, if you were to load everything into ram make sure you know what you are doing and if the arduino resets then this is the reason.

Mushfiq

As noted above, you can open the file for reading and writing, read or seek to a position in the file and overwrite what is there. This is tricky though because you can only overwrite, not insert. i.e. if you have something like:

speed=123altitude=4900

and you want to replace the 123 with 1023, you have a problem because doing so will overwrite the 'a' of altitude. A fixed width file format will make this easier.

Thank you all for your replies.

So if I understand correctly overwriting a line is possible. Will someone be kind enough to show me some sample code of seek and write source?

I already wrote functions for reading a specific line. Each line contains only one value. And it is in " " brackets. So I also have a function that extracts info from those brackets where ever they are.

For example:

language = "eng.txt"
settings = "1"

What i would like to do is go to line (1) and change value to lets say (0) , I don't need to insert new lines, just modify some values.

Thank you

What i would like to do is go to line (1) and change value to lets say (0) , I don't need to insert new lines, just modify some values.

If the new value is the same size as the old value, this is easy. If the new value is longer or shorter than the old value, then it becomes complicated.

That is why is was suggested that you use fixed length records.

Thank you all for your help.

I decided since all of config settings get stored in global variables, that when I modify a value in global variable, I call rewrite() function. This function erases config.txt and rewrites it from scratch (getting values from global variables). Seems to me this is the easiest way...

Good thing I am using Mega2560 otherwise I could run out of memory...

Best regards

Nick Gammon up above You said
"A more usual approach is to open a temporary file, copy line by line from the original to the new file, amending as required. Then delete the old file and rename the new one."
Do you have an example code of this? I am so green when it comes to C coding!

I am so green when it comes to C coding!

The only way to get to a different color is to practice. Here's a great opportunity to practice. Don't blow it.

Hi Guys,

I am having a similar problem with reading line from a file in SD card.

Has anyone figured out how to read a specific line from the .txt file created in SD card. I do not want to modify the line, Just read a line, like line 3 or line 20, and then display it on the serial monitor.

I have this data in a .txt file in SD card, however I want to read a specific line and display on serial monitor.

2018/3/12 15:30 25.81,41.72,1007.63,12057,25.0,AQ = H,260,257,0.02,0.00,0.00,0.04,0.04,0.63,S
2018/3/12 15:35 25.81,41.72,1007.63,12057,25.0,AQ = H,260,257,0.02,0.00,0.00,0.04,0.04,0.63,N
2018/3/12 15:40 25.81,41.72,1007.63,12057,25.0,AQ = H,260,257,0.02,0.00,0.00,0.04,0.04,0.63,E

Thanks
Vishal