<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Can&#x27;t add data to existing SD file]]></title><description><![CDATA[<p dir="auto">Hi,<br />
I have ported my datalogging program from Arduino Mega to the M5Stack Core 2 for AWS.<br />
It all is now working as I expected except for one behavior.<br />
On the Arduino, I could open an existing file on SD card and append data to the end of the file.<br />
On the M5Stack, I can create, open, write to, and close a file on the SD card.<br />
But when I re-open this file and write more data, it overwrites the existing data in the file.<br />
It appears the FILE_WRITE specifier is allowing write access but is not moving the file pointer to its end...not sure why not?<br />
Can anyone suggest what I am doing wrong or provide a workaround?<br />
Thanks!</p>
<p dir="auto">The relevant portion of my test code is below; apologies if it comes through with weird formatting:</p>
<p dir="auto">if (!SD.begin(4,SPI,40000000)) {   // ******M5 CS line is GPIO4<br />
Serial.println("initialization failed!");<br />
while (1);<br />
}<br />
Serial.println("initialization done.");</p>
<p dir="auto">// open the file. note that only one file can be open at a time,<br />
// so you have to close this one before opening another.</p>
<pre><code>NewFile = SD.open("/TRIAL.TXT",FILE_WRITE);  
char message1[64]="This is just a test";  // 64 byte array
char message2[64]="It seems to be working so far!";
</code></pre>
<p dir="auto">//  delay(5000);<br />
// if the file opened okay, write to it:<br />
if (NewFile) {<br />
NewFile.println(message1);<br />
NewFile.println(message2);</p>
<pre><code>// close the file:
NewFile.close();
</code></pre>
<p dir="auto">} else {<br />
// if the file didn't open, print an error:<br />
Serial.println("error initially opening test.txt");<br />
}</p>
<p dir="auto">// Now try reopening file and writing more data into it</p>
<pre><code> delay(100);
 NewFile = SD.open("/TRIAL.TXT",FILE_WRITE);  //*******Reopen for more writing
 NewFile.println("Trying to add this additional line");
 NewFile.close();
 delay(100);
</code></pre>
]]></description><link>https://community.m5stack.com/topic/4184/can-t-add-data-to-existing-sd-file</link><generator>RSS for Node</generator><lastBuildDate>Thu, 07 May 2026 00:20:57 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4184.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 05 Apr 2022 12:19:50 GMT</pubDate><ttl>60</ttl></channel></rss>