user29266: I want to write a variable value into a text file in the SD card and modify it while reading if needed.
I found a text reading code which reads a desired line in the text file.(not tested)
//reading code
#include <SD.h>

int stepToLoad = 1;
int endOfLineNumber = 0;
int stepCount = 0;
int stringIndex = 0;
File configFile;
char inputString [1280];
char inputChar;

void setup() {
  stepToLoad = 13;
  if (!SD.begin(36)) {
    return;
  }

  if (SD.exists("config.txt")) {
    configFile = SD.open("config.txt");
    getStepCount();
    getStepData();
    configFile.close();
  }
}

void loop() {

}

void getStepData() {

  if (configFile) {
    while (configFile.available()) {
      inputChar = configFile.read(); // Gets one byte from serial buffer
      if (inputChar != '^') { // define breaking char here

        inputString[stringIndex] = inputChar; // Store it
        stringIndex++; // Increment where to write next

      } else {
        endOfLineNumber++;
        stringIndex++;
        if (endOfLineNumber == stepToLoad) { //this is the correct line to load


          break; //break out of the "while" loop
        } else { //not the correct line to load, reset and continue

          stringIndex = 0;
        }
      }

    }

  }
}

void getStepCount() {
  if (configFile) {
    while (configFile.available()) {
      inputChar = configFile.read(); // Gets one byte from serial buffer
      if (inputChar == '^') { // define breaking char here
        stepCount++;
      }
    }
  }
}


user31105: As far as I know it is not possible to read a file and meanwhile change a value.
What normally is done is:

Read the file
Store it in memory
Change the value in memory
Write the file

However, since the SRAM memory is very limited, the following approach is better:

For every buffer (512 bytes):


Read 512 bytes buffer
Change values in the buffer if needed
Write 512 bytes to another file
Until the end of the file to read is reached

Remove the original file
Change the name of the new file to the old file


user13432: 
How to write a variable value into a text file and modify it in the SD card?

This software design issue depends much on the chosen representation on file. In principle, there are two design dimensions. The first is textual or binary representation. The second is fixed or variable size records.
With textual representation (e.g. Serial.print) values are converted to text and stored on the file. The size of the record (e.g. line) depends on the number of characters needed for the value. New-line is often used as the record delimiter. To locate a specific record the file has to be scanned. There is no way to calculate the position of a record. When reading back the value the textual representation must be parsed and the internal representation in memory restored. 
With binary representation (e.g. Serial.write) values are written directly from memory to the file. The size of the record is fixed as the size of a struct. Locating a specific record is easy; record index time record size. The SD library file.seek(position) is perfect to index the record. When reading the value back to memory there is no need for conversion as in textual representation.
It should be added that fixed size textual representation is also possible, e.g. make each line the same length by using a padding character (space, etc).  
A text file with variable values is the more difficult as it is both textual representation and variable record. It is also the most inefficient as it requires compute power for the value representation conversions. The major advantage is that the text file can be generated with a ordinary text editor. No special tool or program is needed. 
Binary file format with fixed records will require initialization.
Cheers!

29266: <p>I want to write a variable value into a text file in the SD card and modify it while reading if needed.
I found a text reading code which reads a desired line in the text file.(not tested)</p>

<p>//reading code</p>

<pre><code>#include &lt;SD.h&gt;

int stepToLoad = 1;
int endOfLineNumber = 0;
int stepCount = 0;
int stringIndex = 0;
File configFile;
char inputString [1280];
char inputChar;

void setup() {
  stepToLoad = 13;
  if (!SD.begin(36)) {
    return;
  }

  if (SD.exists("config.txt")) {
    configFile = SD.open("config.txt");
    getStepCount();
    getStepData();
    configFile.close();
  }
}

void loop() {

}

void getStepData() {

  if (configFile) {
    while (configFile.available()) {
      inputChar = configFile.read(); // Gets one byte from serial buffer
      if (inputChar != '^') { // define breaking char here

        inputString[stringIndex] = inputChar; // Store it
        stringIndex++; // Increment where to write next

      } else {
        endOfLineNumber++;
        stringIndex++;
        if (endOfLineNumber == stepToLoad) { //this is the correct line to load


          break; //break out of the "while" loop
        } else { //not the correct line to load, reset and continue

          stringIndex = 0;
        }
      }

    }

  }
}

void getStepCount() {
  if (configFile) {
    while (configFile.available()) {
      inputChar = configFile.read(); // Gets one byte from serial buffer
      if (inputChar == '^') { // define breaking char here
        stepCount++;
      }
    }
  }
}
</code></pre>
 31105: <p>As far as I know it is not possible to read a file and meanwhile change a value.</p>

<p>What normally is done is:</p>

<ul>
<li>Read the file</li>
<li>Store it in memory</li>
<li>Change the value in memory</li>
<li>Write the file</li>
</ul>

<p>However, since the SRAM memory is very limited, the following approach is better:</p>

<ul>
<li>For every buffer (512 bytes):

<ul>
<li>Read 512 bytes buffer</li>
<li>Change values in the buffer if needed</li>
<li>Write 512 bytes to another file</li>
<li>Until the end of the file to read is reached</li>
</ul></li>
<li>Remove the original file</li>
<li>Change the name of the new file to the old file</li>
</ul>
 13432: <blockquote>
  <p>How to write a variable value into a text file and modify it in the SD card?</p>
</blockquote>

<p>This software design issue depends much on the chosen representation on file. In principle, there are two design dimensions. The first is textual or binary representation. The second is fixed or variable size records.</p>

<p>With textual representation (e.g. Serial.print) values are converted to text and stored on the file. The size of the record (e.g. line) depends on the number of characters needed for the value. New-line is often used as the record delimiter. To locate a specific record the file has to be scanned. There is no way to calculate the position of a record. When reading back the value the textual representation must be parsed and the internal representation in memory restored. </p>

<p>With binary representation (e.g. Serial.write) values are written directly from memory to the file. The size of the record is fixed as the size of a struct. Locating a specific record is easy; record index time record size. The SD library file.seek(position) is perfect to index the record. When reading the value back to memory there is no need for conversion as in textual representation.</p>

<p>It should be added that fixed size textual representation is also possible, e.g. make each line the same length by using a padding character (space, etc).  </p>

<p>A text file with variable values is the more difficult as it is both textual representation and variable record. It is also the most inefficient as it requires compute power for the value representation conversions. The major advantage is that the text file can be generated with a ordinary text editor. No special tool or program is needed. </p>

<p>Binary file format with fixed records will require initialization.</p>

<p>Cheers!</p>
