Reading a word over an Int

Hello everyone

I'm using the code from A Simple Function for Reading CSV Text Files. - Storage - Arduino Forum I wasn't able to post on this topic given it was over 120 days old with no replay, it suggested I create a new topic.

I'm using the code as follows

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

#define CS_PIN BUILTIN_SDCARD

// 5 X 4 array
#define ROW_DIM 5
#define COL_DIM 4

File file;

/*
 * Read a file one field at a time.
 *
 * file - File to read.
 *
 * str - Character array for the field.
 *
 * size - Size of str array.
 *
 * delim - String containing field delimiters.
 *
 * return - length of field including terminating delimiter.
 *
 * Note, the last character of str will not be a delimiter if
 * a read error occurs, the field is too long, or the file
 * does not end with a delimiter.  Consider this an error
 * if not at end-of-file.
 *
 */
size_t readField(File* file, char* str, size_t size, char* delim) {
  char ch;
  size_t n = 0;
  while ((n + 1) < size && file->read(&ch, 1) == 1) {
    // Delete CR.
    if (ch == '\r') {
      continue;
    }
    str[n++] = ch;
    if (strchr(delim, ch)) {
        break;
    }
  }
  str[n] = '\0';
  return n;
}
//------------------------------------------------------------------------------
#define errorHalt(msg) {Serial.println(F(msg)); while(1);}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  SDcard0.int_SD();
  // Initialize the SD.
  if (!SD.begin(CS_PIN)) {
    errorHalt("begin failed");
  }
  // Create or open the file.
  file = SD.open("12READNUM.TXT", FILE_WRITE);
  if (!file) {
    errorHalt("open failed");
  }
  // Rewind file so test data is not appended.
  file.seek(0);

  // Write test data.
  file.print(F(
    "11,12,13,14\r\n"
    "21,22,23,24\r\n"
    "31,32,33,34\r\n"
    "41,42,43,44\r\n"
    "51,52,53,54"     // Allow missing endl at eof.
    ));

  // Rewind the file for read.
  file.seek(0);

  // Array for data.
  int array[ROW_DIM][COL_DIM];
  int i = 0;     // First array index.
  int j = 0;     // Second array index
  size_t n;      // Length of returned field with delimiter.
  char str[20];  // Must hold longest field with delimiter and zero byte.
  char *ptr;     // Test for valid field.

  // Read the file and store the data.
  
  for (i = 0; i < ROW_DIM; i++) {
    for (j = 0; j < COL_DIM; j++) {
      n = readField(&file, str, sizeof(str), ",\n");
      if (n == 0) {
        errorHalt("Too few lines");
      }
      array[i][j] = strtol(str, &ptr, 10);
      if (ptr == str) {
        errorHalt("bad number");
      }      
      if (j < (COL_DIM-1) && str[n-1] != ',') {
        errorHalt("line with too few fields");
      }
    }
    // Allow missing endl at eof.
    if (str[n-1] != '\n' && file.available()) {
      errorHalt("missing endl");
    }    
  }

  // Print the array.
  for (i = 0; i < ROW_DIM; i++) {
    for (j = 0; j < COL_DIM; j++) {
      if (j) {
        Serial.print(' ');
      }
      Serial.print(array[i][j]);
    }
    Serial.println();
  }
  Serial.println("Done");
  file.close();
}
//------------------------------------------------------------------------------
void loop() {
}

The code works great, how ever I'm looking to have a file with some name to it. I know it looks like a mess but this is a puzzle layout for a chess board. The goal here would be to read in the file then map the cells into a 2x2 array.

8 BK
7 BP BQ BP
6 BP WR BP
5 WB BP
4 BK BR
3 WP WP WP
2 WP WP WQ
1 WK
a b c d e f g h
WR6gh6 BPg7h6 WQg2g8
Whites Move

does anyone know how to convert the array*[j] into an array that will allow for numbers and letters. *
When I change any of the code above to read in a file with a letter I get the error message bad number.
Thank you in advanced any help would be great,
Joe

Dellyjoe:
does anyone know how to convert the array*[j] into an array that will allow for numbers and letters.*
[/quote]
In the ASCII table, numbers have values, 0x30 representing 0 to 0x39 representing 9. Letters are from 0x41 to 0x5A for A to Z.
So, if it was me, I'd make myArray[ i ][ j ] store bytes.

You could reduce this to an array of Strings, one for each row, and use the rich set of String operators to retrieve your individual fields.

Dellyjoe:
does anyone know how to convert the array*[j] into an array that will allow for numbers and letters.*
[/quote]
here's some code that appears to read you file.
board is a matrix of short char strings.
```
*//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

char        *progname;
unsigned int dbg  = 0;
unsigned int flag  = 0;

// --------------------------------------------------------------------
#define N_ROW  8
#define N_COL  8
struct Sq_s {
    char s [3];
} board [N_ROW][N_COL] = {};

// ----------------------------
void boardDisp (void)
{
    for (int row = 0; row < N_ROW; row++)  {
        printf (" %d:", row+1);
        for (int col = 0; col < N_COL; col++)
            printf (" %2s", board [row][col].s);
        printf ("\n");
    }
}

// ----------------------------
void
load (
    FILE *fp)
{
    char  buf [BUFSIZ];
    char *fld [10];;
    int  n;
    char *p;
    char *q;
    int  row = 8;

while (fgets (buf, BUFSIZ, fp) != NULL)  {
        buf [strlen (buf)-1] = 0;
        if (dbg) printf (" %s: %s\n", func, p);

for (n = 0, p = buf; q  = strchr (p, '\t') ; p = q+1, n++)  {
            *q = 0;                // replace tab with NULL
            fld [n] = p;
            if (dbg) printf (" %d %s\n", n, fld [n]);
        }
        fld [n] = p;
        if (dbg) printf (" %d %s\n", n, fld [n]);

if (8 == n)  {
            for (int i = 1; i <= 8; i++)
                if (0 != *fld [i])  {
                    if (dbg) printf (" %2s", fld [i]);
                    strcpy (board [row][i-1].s, fld [i]);
                }
            if (dbg) printf ("\n");
            row--;
        }
    }

boardDisp ();
}

// --------------------------------------------------------------------
int main (int argc, char **argv)  {
    FILE fp;
    if ( (fp = fopen (
++argv, "rb")) == NULL)  {
        perror ("app - fopen input");
        exit (1);
    }

load (fp);
    return 0;
}*

```

Hello gcjr, and jrdoner atm, I'm a little overwhelmed and seem that I can't understand the code that was written below.

I will have to take some time to go over this but it seems more complicated then I hoped for.

Thanks for the written explanations I will try and learn the code provided.

Jrdoner I will look to see how to go about converting the array to a string.

Thank you both,
Joe

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