How to set a char array to a specific spot in a 2d char array

I'm still trying to figure out Arduino code the only coding language I know really well is python

here's my code pasted into the foroms directly

#include <SD.h>

int chipSelect = 4;
char bdaylist[2][6][13];// this use to be bdaylist[50][6][13] buy with strncpy i had to chaenge it
File file;

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

  pinMode(chipSelect, OUTPUT);
  while(true){
    if (SD.begin(chipSelect)){
      Serial.println("SD Card initialize.");
      break;
    }else{
      Serial.println("Could not initialize SD Card.");
    }
  }
  if(SD.exists("bdays.txt")){
    Serial.println("card contanse the birthday list");
    file = SD.open("bdays.txt", FILE_READ);
    char character;
    char charBuf[13];
    String word;
    int row = 0;
    int col = 0;
    while ((character = file.read()) != -1){
      if(character == '|'){
        Serial.println();
        Serial.print("word = ");
        Serial.println(word);
        word.toCharArray(charBuf, 13);
        Serial.print("charBuf = ");
        Serial.println(charBuf);
        word = "";
        
        Serial.println();
        Serial.print("bdaylist[");
        Serial.print(row);
        Serial.print("][");
        Serial.print(col);
        Serial.print("] = ");
        
        //strcpy(&bdaylist[0][1], charBuf, sizeof(charBuf));
        strncpy(bdaylist[row][col], charBuf, sizeof(charBuf));
        Serial.println(bdaylist[row][col]);
        col++;
      }
      else if(character == '\n'){
        Serial.println();
        Serial.print("word = ");
        Serial.println(word);
        word.toCharArray(charBuf, 13);
        Serial.print("charBuf = ");
        Serial.println(charBuf);
        word = "";
        
        Serial.println();
        Serial.print("bdaylist[");
        Serial.print(row);
        Serial.print("][");
        Serial.print(col);
        Serial.print("] = ");
        //strcpy(&bdaylist[0][1], charBuf, sizeof(charBuf));
        strncpy(bdaylist[row][col], charBuf, sizeof(charBuf));
        Serial.print(bdaylist[row][col]);
        
        //Serial.println();
        row++;
      }else{
        word = word+character;
      }
      //Serial.println();
    }
    Serial.println();
    file.close();
    Serial.println("--reading ended--");
    
    for(int r=0;r<2;r++){
      for(int c=0;c<6;c++){
        Serial.print("<");
        Serial.print(bdaylist[r][c]);
        Serial.print(">\t");
      }
      Serial.println();
    }
    
    Serial.println("--end--");
  }
}

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

}

strcpy doesn’t take 3 arguments

Don’t use the size with strcpy

Basically you want to use this strlcpy which is safer than strcpy and strncpy

size_t strlcpy( char *dst,
                const char *src,
                size_t size );

See https://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/s/strlcat.html for important notes

So you need the pointer to somewhere in memory where you have enough bytes to copy your charbuf content including the trailing null.

If you need a 2D array containing the texts, then indeed you need to define a 3D array, the last dimension being for the bytes of the data

const size_t nbCols = 30;
const size_t nbRows = 10;
const size_t maxTextLength = 12;
char storage[nbRows][nbCols][maxTextLength+1]; // +1 for the trailing null char

Note that this array uses up 30x10x12 bytes that is 3600 bytes. Depending on your arduino it’s a lot, It won’t fit on a UNO for example.

The destination pointer to the memory location where you have the bytes available can be given by the first part of the array, for example

storage[3][5]

Or by taking the address or the first byte

&(storage[3][5][0])

This would be the address of 4th row and 7th column (indexes start at 0).

And if you use strlcpy() then the size at that location is maxTextLength+1

To fill up the charbuf array you have only maxTextLength bytes available and you keep the +1 byte always for the trailing null if your text reaches max capacity

I figured it out I am reading only one line of the text file on the SD card instead of storing it into a 3d array thanks for the help.

this forum is solved no need to post anymore. thank you all. good bye.

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