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