String compare from SD card .txt file

Good day All,

My current project requires reading values from Ultrasonic sensors and writing them to a .txt file on a SD card. However upon first start up the an initial heading is printed in the .txt file in the setup() section. Now this is where I need help. Should the project be powered off and on again I don't want the heading to be written again. Is there a way to write an if statement to read from the .txt. file to prevent the heading from being written again to the SD card. I saw that strncmp can be used in such a case, but could not yet have figured it out. I was hoping someone could help me with an example of something similar. This is the code with strncmp that I tried but could not get working (Given the title was already printed to the .txt file):

 myFile ="Diesel log.txt", FILE_WRITE);
        Serial.println("FOund line");
        Serial.println("No line");

  myFile.println("SGD BOERDERY DIESEL LOG");
  myFile.print("TIME LOGGED");
  myFile.print("TANK 1:");
  myFile.println("TANK 2:");

Your help is much appreciated.

change your strategy:

first check if the file exists
if the file doesn't exist - open the file and write your header (I assume you only need it for a new file)
if the file exist - open it and don't write the header and continue with your logging

Hi @noiasca ,

Great advice. Will give it a try and give feedback.
Thanks a lot!

You could also open the file and use file.size() to see if anything has been written to the file. If the size is 0, write the header.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.