Strncat() not doing the "cat" part

Having an issue with strncat(). It doesn't seem to be doing what its supposed to. currWord never gets populated (line 66), at least not with character data. I have a similar program that uses it just fine.

This is reading in a file, parsing it, and trying to load it into an array.

This is the part that doesn't work

default:
          strncat(currWord, currChar,1);
          Serial.print(currChar);
          Serial.print('-');
          Serial.println(currWord);
          break;

Lots of prints for troubleshooting in here, sorry. I've been staring at it and trying nonsense for hours...

#include <SPI.h>
#include <SD.h>


// change this to match your SD shield or module;
const int chipSelect = 10;
int array_size = 0;
double steps[10][5];
File myFile;

void setup() {
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  readSD();
}

void loop() {

}

void readSD() {
 
  int array_size = 0;
  int arraycount = 0;
  int col, row = 0;
  char currChar;
  char currWord[8] = {"\0"};

  // READ FROM CARD

  myFile = SD.open("1.txt");
  if (myFile) {

    // READ VALUES TO ARRAY

    while (myFile.available()) {
      currChar = myFile.read();
      
      //Serial.print(currChar);
      switch (currChar) {
        case ',':
         // Serial.print(currWord);
          //Serial.print(',');
          steps[row][col] = atof(currWord);
          strcpy(currWord,"\0");
          col++;
          break;
        case '\r':
         // Serial.println(currWord);
          steps[row][col] = atof(currWord);
          strcpy(currWord,"\0");
          row++;
          col=0;
          myFile.read();
          //Serial.println();
          break;
        default:
          strncat(currWord, currChar,1);
          Serial.print(currChar);
          Serial.print('-');
          Serial.println(currWord);
          break;
      }
    }
  }
  array_size = row;
  myFile.close();
  Serial.println("file closed");
  for (int i = 0; i <= row; i++){
    for (int j = 0; j <= 5; j++){
      Serial.print(steps[i][j]);
      Serial.print(',');
    }
    Serial.println();
  }
}

this is the data in the file

1,3.555,1,1,76
2,3.675,0,1,76
3,7,2,2,0
4,2.755,1,1,83
5,2.295,1,0,83
6,2.875,0,0,83
7,2.295,0,1,83
8,1.410,0,1,90
9,1.410,1,1,90

this is the output (it wouldn't all paste in for some reason. currWord seems to have non character data that won't copy/paste. I can see the squares in the serial monitor.

Initializing SD card...initialization done.

1-

3-

.-

5-

5-

5-

1-

1-

7-a

6-a

2-

3-

.-

6-

7-a

"currChar" is not a C-string, as the function str(n)cat requires (it expects a pointer, not a value, as the source string argument).

This works:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  char currWord[10] = {0};
  char currChar[] = "a";
  strncat(currWord, currChar, 1);
  Serial.print(currChar);
  Serial.print('-');
  Serial.println(currWord);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Or this:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  char currWord[10] = {0};
  char currChar = 'a';
  strncat(currWord, &currChar, 1);
  Serial.print(currChar);
  Serial.print('-');
  Serial.println(currWord);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Thanks for the response.
to make sure I understand...
this
char currChar[] = "a";
makes currChar a pointer because the double quotes makes a String and setting it = is really establishing a pointer to it, even though currChar wasn't defined as a pointer? Why does it need to be declared as an array?

char currChar = 'a'; is setting a single character variable which is holding only the character a, and then has to be dereferenced as &currChar to supply its address to the function?

OF course I am reading in 1 char at a time via the .read method of File so I assume the 2nd example is the more appropriate one to use.

not the same as passing a c-string which is an array of chars terminated by a Nul '\0'.

a more conventional approach is to read a buffer, the read returns the number of chars read, whether the size of the buffer, something less or 0 at EOF.

chars can then be processed as desired, one at a time or in some blocks possibly delimited by a termination char (e.g. \n)

An array name is a pointer, which is required for strncat().

char currChar[] = "a"; makes currChar a pointer because the double quote

No, the square brackets declare an array.

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