Learning about strcpy


i saw code had neat function, , i'm wondering if possible and/or if benefit me here in example.  make code smaller/neater somehow use strcpy() function here.  i'm not sure need eliminate though.  currently, loop thru string characters , copy them 1 1. (around line 12 in sample)
code: [select]
void next3files() { //opens next file on sd card
  (int = 0; < 3; a++) { //loop thru 3 files
    file entry =  dir.opennextfile();
    if (! entry) { //there isn't next file
      entry.close(); //close entry
      dir.rewinddirectory(); //rewind
      entry =  dir.opennextfile(); //get first file
    }
    entryname = entry.name();
    entry.close();
    int  len = strlen(entryname); //how long name?
    memset(names[a], 0, sizeof(names[a])); //delete previous value. necessary?
    (int = 0; < len; i++) {
      names[a][i] = entryname[i];

    }
  }
}


then, when print 3 names lcd display use code.  there way print name, without having loop thru each character, or i'm doing best way? (about line 12 , 13)
code: [select]
void select_part() {
  { //keep looping through names until select one
    next3files(); //get next 3 file names
    disp.lcd_cls(); //clear display
    (int = 0; < 3; a++) { //loop thru 3 names
      int  len = strlen(names[a]); //how long name?
      disp.lcd_rowcol(a, 0); //row, col
      disp.lcd_print(a + 1); //the selection number
      disp.lcd_print(f(": ")); //the selection number suffix
      debug_print(a + 1); //the selection number
      debug_print(": ");  //the selection number suffix
      (int = 0; < len - 4; i++) {
        disp.lcd_print(names[a][i]); //the filename
        debug_print(names[a][i]); //the filename
      }
      debug_println();
    }
    disp.lcd_print("4. more  5. cancel"); //4 or 5 see more file names of cancel
    debug_println("4. more  5. cancel"); //4 or 5 see more file names of cancel
    debug_println();
    while (1) { //wait key input
      byte k = checkkeybuffer(); //get keypress user
      switch (k) {
        case 1:
          drillhole(names[0], 1); //drill selection 1
          return; //after drilling part, we'll return main menu
        case 2:
          drillhole(names[1], 1); //drill selection 2
          return; //after drilling part, we'll return main menu
        case 3:
          drillhole(names[2], 1); //drill selection 3
          return; //after drilling part, we'll return main menu
        case 4:
          //show next 3 files
          break; //exit key input loop show next 3 entries
        case 5: //cancel
          return;
        case keytimeout: //user input timed out
          return;
      }
    }
  }
  while (1); //until break
}

without knowing 'disp' is, it's difficult answer second question. 'normal' lcd, can print string
code: [select]
// print name on lcd
lcd.print(names[a]);
// go next line on lcd
lcd.setcursor(n,0);


for first question, you're not copying terminating nul character. answer comment 'is necessary' yes. if use strcpy(), don't need strlen(), memset() , don't need loop.
code: [select]
strcpy(names[a], entryname);


note:
not tested.


Arduino Forum > Using Arduino > Programming Questions > Learning about strcpy


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'