SDfat Reading/Write

Hi everyone one. I am just learning how to read and write from sd card to Arduino.

I am using SDfat library.
My objective is to skip all the lines that contains "Deleted" and just continue reading on the next line.

On my SD card I have:

Line 1
Line 2
Line 3
Line 4
Line 5 and so on.....

When I run my program. I couldn't make Arduino ignore the line that contain "Deleted". It always include the line that contain "Deleted" during the loop cycle.

Deleted
Line 2
Line 3
Line 4
Line 5 and so on.....

On the next cycle. I only wanted to read.

Line 2
Line 3
Line 4
Line 5 and so on.....

I have attached the codes that I'am using. Please show me the right codes to make it happen. Thank you so much.

#include <SPI.h>
#include "SdFat.h"
#include "sdios.h"



const uint8_t chipSelect = SS;

SdFat sd;
// print stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash memory
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
void demoFgets() {
 char line[100];
 int n;
String buffer = line;
 // open test file
 SdFile rdfile("fgets.txt", O_RDONLY);

 // check for open error
 if (!rdfile.isOpen()) {
   error("demoFgets");
 }


 while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
   if (line[n - 1] == '\n') {
  cout << line;
   }
  else (line == "Deleted"); {
continue;
  }
 }
}
//------------------------------------------------------------------------------
void makeTestFile() {
 // create or open test file
 SdFile wrfile("fgets.txt", O_WRONLY | O_CREAT | O_TRUNC);

 // check for open error
 if (!wrfile.isOpen()) {
   error("MakeTestFile");
 }

 // write test file
 wrfile.print(F(
                "Deleted\r\n"
                "Line B\r\n"
                "Line C\r\n"
                "Line D\r\n"
                "Line E\r\n"
              ));
 wrfile.close();
}
//------------------------------------------------------------------------------
void setup(void) {
 Serial.begin(9600);
 
 // Wait for USB Serial 
 while (!Serial) {
   SysCall::yield();
 }

 cout << F("Type any character to start\n");
 while (!Serial.available()) {
   SysCall::yield();
 }
 delay(400);  // catch Due reset problem

 
 if (!sd.begin(4)) {
   sd.initErrorHalt();
 }

 makeTestFile();


}
void loop(void) 
{
  demoFgets();
   delay(5000);
 }

SDfatRW.txt (1.79 KB)

A couple of questions :

(1) Which Arduino board are you using ?

(2) What is this line of code intended to do ?

   else (line == "Deleted");

UKHeliBob:
A couple of questions :

(1) Which Arduino board are you using ?

(2) What is this line of code intended to do ?

   else (line == "Deleted");
  1. I'am using Arduino uno r3

  2.   else (line == "Deleted"); = I thought using this code will make the reading skip the lines that contains "Deleted" but it didn't work.

Please edit your post to add code tags, as described in How to use this forum.

Don't you need a statement in there somewhere to test the contents of line and to act on the result of the test ?

Bear in mind that you cannot compare strings (lowercase s) using an if. The strcmp() function can be used for such comparisons

UKHeliBob:
Don't you need a statement in there somewhere to test the contents of line and to act on the result of the test ?

Bear in mind that you cannot compare strings (lowercase s) using an if. The strcmp() function can be used for such comparisons

Can you give me some example please that I can follow. May be if I can see how it's done I will have a clear idea how the codes flow.

jremington:
Please edit your post to add code tags, as described in How to use this forum.

ok I apologize for that.

Lots of similar tutorials on line.

The first thing to do is to read a line of text. The fgets() function does that and puts the line into the buffer provided, However, the buffer is just an array of chars and there is nothing special about it. It is not at that stage a C style string (lowercase s) because it is not terminated by a zero

I believe that your demoFgets() function is based on this function

void demoFgets() {
  char line[25];
  int n;
  // open test file
  SdFile file("FGETS.TXT", O_READ);
 
  // check for open error
  if (!file.isOpen()) error("demoFgets");

  // read lines from the file
  while ((n = file.fgets(line, sizeof(line))) > 0) {
    if (line[n - 1] == '\n') {
      // remove '\n'
      line[n-1] = 0;
      // replace next line with LCD call to display line
      Serial.println(line);
    } else {
      // no '\n' - line too long or missing '\n' at EOF
      // handle error
    }
  }
}

Taken from How to read a TXT file by line? - Storage - Arduino Forum

Note how the '\n' at the end of the file is replaced by a zero to turn the line array into a string

Once the whole line is in a string you can compare it to a string constant like this

if (strcmp(line, "Deleted"))  <<<<<< WRONG - see subsequent posts for a correction to this code
  {
    //do something if a match is found
  }

Doesn't the strcmp() function returns 0 on a match.

if (strcmp(line, "Deleted"))
{
//do something if a match is found
}

Doesn't the strcmp() function returns 0 on a match.

Whoops on my part
The most explicit way of coding the comparison is

if (strcmp(line, "Deleted") == 0)
  {
    //do something if a match is found
  }

Thank you so much for all your inputs. I really appreciate it. I will try all your suggestions and references. It seems that I still need to read some more materials to learn more. :slight_smile: