Trouble integrating SD libary

TheMemberFormerlyKnownAsAWOL:
And the size of "output" is ...?

would it not be the size of the array that is dereferenced?

No.
Hence “oops”

TheMemberFormerlyKnownAsAWOL:
No.
Hence "oops"

My bad.
Well, I made a bad wrapper, but I wouldn't use a wrapper anyway. Just use the string functions, as aarg pointed out, which was sort of my point inferred.

Thanks. Honestly really my bad, I would have used the c functions directly if I had known they existed in the first place. BTW, I already replaced my copyChars and charsAreEqual, but unluckily it makes little difference, there is still some mess somewhere else I need to untangle. I am also quite confident my implementation of substring() is another good candidate for debugging, as you might have guessed I am still learning my ways around c arrays.

At any rate, I guess I will reimplement my parser from scratch, for something is definitely rotten there.
I will also try to use simpler models, relying more on int values rather than strings.

Thanks again.

nutsmuggler:
At any rate, I guess I will reimplement my parser from scratch, for something is definitely rotten there.

Might I suggest sidelining this project if you have the time, and working through the string.h library tutorials rather than reinventing the wheel? In your functions, I noticed a lot of for loops to iterate over an array. Going from a 5 GHz mutli-threaded 64-bit processor to a 16 MHz single-threaded 8-bit processor, you lose a lot of headroom, for things like for loops and conditionals. Instead of nesting conditionals, you might see a lot of conditions aggregated into a single condition. The string library has been around for decades, and creates very clean, fast and efficient code. Write several small sketches working with just the serial monitor. Doing so will have dividends in the larger, memory abundant coding as well.

Yes, that seems a good idea.
I realize I am approaching this microcontroller from the wrong angle. In my line of work all the effort is in the larger architecture (questions like: “should this view controller own the navigation flow or not?”) , because system libraries and ARC (Apple’s fancy implementation of garbage collection) have already solved many of the basic problems for us. I am gradually learning that in this field attention to memory allocation is paramount.

An I do realize I have a problem with basic char arrays, which is the definition of a fundamental problem.

By the way, I rewrote the core routine using a much simpler and streamlined logic, working with structs and ints mostly. I sidelined the parser for now, I want to better understand how to manipulate strings. with basic c functions. In my current implementation I am feeding program data manually, that is, allocating the structs and populating them explicitly. At least I have the basic routine working.

I returned to this thread because I am having issues with simple code, and it is still very SD related.
I have made a super basic wrapper around the SD libary, for writing and reading into char arrays.
My code mostly works: I write a string on an SD card file. Yet the Serial monitor output is somehow messed up. I’d like t know f I am doing something wrong.

Here is the setup() code which I am using fr testing my functions:

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  Serial.println("<Arduino is ready>");

  sdManager.setup();

  Serial.println("--> WRITE");

  char filename[] = "p_01.log";
  char content[] = "2020-06-26";

  sdManager.writeFile(filename,content,true);

  Serial.println("--> READ");
  char output[10];
  sdManager.readFile(filename, output);

  Serial.print("READ output: -");

  Serial.print(output);

  Serial.println("-...and <DONE>");
  int i = strlen(output);
  Serial.println(i);
  
}

And here is the SDManager class. As you might notice, most of the code is lifted from the simple ReadWrite SD Example in the Arduino library.

/*
  SDManager.cpp- Library for managing BLE inpuut/output data.
  Created by Davide Benini, 17/06/2020.
  MIT license.
*/
#include "SDManager.h"
#include <Arduino.h>

const int chipSelect = 10;
#define FILE_WRITE_TRUNCATE (O_READ | O_WRITE | O_CREAT | O_TRUNC)


void SDManager::setup() {
    
    Serial.print(F("Initializing SD card ... "));
    // see if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
        Serial.println(F("Card failed, or not present"));
        // don't do anything more:
        while (1);
    }
    Serial.println(F("card initialized."));
}


void SDManager::writeFile(char* filename, char* content, bool truncate) {
 
  Serial.println(F("Writing..."));

  uint8_t mode = truncate ? FILE_WRITE_TRUNCATE : FILE_WRITE;
  
  File dataFile = SD.open(filename, mode);
       // if the file is available, write to it:
  if (dataFile) {
    Serial.println(F("File found"));
    dataFile.print(content);
    
    dataFile.close(); 
    // print to the serial port too:
    //Serial.print(content);
  } else {
    Serial.println(F("error opening "));
    Serial.println(filename);
  }
}

bool SDManager::fileExists(char* filename) {

  return SD.exists(filename);
}

void SDManager::readFile(char* filename, char* output) {
  
  Serial.println(F("Reading..."));

  File myFile = SD.open(filename);
  
  
  if (myFile) {
    //Serial.println(filename);
    // read from the file until there's nothing else in it:
    int index = 0;
    while (myFile.available()) {
      //result = result +  myFile.read();
      char ltr = myFile.read();
      output[index] = ltr;
      //Serial.println(ltr);
      index++;
    }
    output = output + '\0';
    // close the file:
    myFile.close();
  }
}

And finally my serial monitor output:

<Arduino is ready>
Initializing SD card ... card initialized.
--> WRITE
Writing...
File found
--> READ
Reading...
READ output: -2020-06-26p_01.log-...and <DONE>
18

You might have noticed that the filename variable gets printed after the output variable.
This seems to be a logging errror; if I inspect the file I see only “2020-06-26”, the correct content.
What is more, if I just read the file, commenting out the “sdManager.writeFile()”, the serial monitor prints the correct output (READ output: -2020-06-26-…and ).

I’d like to know whether I should ignore this (some serial interference?) or if I am doing something in a wrong/non safe way.

Thanks in advance,
Davide

Hello folks,
today I picked up the same code I shared, and it worked.
So I. guess it was some serial/USB interference in the end...
I am beginning to suspect there is something wrong with my Uno board, I get. strange behaviors even with this kind of minimal examples. At any rate, I'll have to buy a new board for when I deploy this project, I guess I'll get one and see what happens.
Cheers,
Davide

Just a quick update.
After some trouble writing in the SD I switched from the SD library to SdFat, and boom, the algorithm works like a charm. Now I write and read reliably from the SD card, while before, for no apparent reason, both writing and reading functions worked unpredictably.
I guess some of the leakage I saw before were due to SD.h.