Go Down

Topic: Reading .txt files from SD card (Read 2292 times) previous topic - next topic



i want to read data from a txt file stored in sd card with arduino(uno)
the file structure is like this:
[2 2 3 4 5
3 1 5 8 2
4 4 5 6 7
4 5 6 8 1]
and then to store the values in an array (matrix, for this example a matrix[4][5])
to use them in functions.
is there a command i can use or some code to do so? if convinient explain please
if it is more helpful i will use comma seperated values
any suggestions acceptable...


You really don't need to have any structure to the file if all you are doing is saving a bunch of numbers, as how you read them in creates the structure in the code. I would not bother with the '[' and ']'. I am assuming you know how to open a file for reading or can at least find out.

I would start by
Code: [Select]
for (uint8_t i = 0; i<ROW_SIZE; i++)
  for (j=0; j<COL_SIZE, j++)
    array[i,j] = readValue();

Then just write a readValue() function that reads the next element of data from the file. The logic for the function looks something like this:
Code: [Select]
if end-of-file return(0);
read characters while they are not a number
num = character - '0'; // this creates a single digit number from the ASCII character
while the characters are numbers
num = (num * 10) + character - '0';

You may also find a function in the SD library that you are using that could read the next number for you - that depends on the library.

To write the numbers is very similar - use the loop and write the values instead of reading them.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


That was very helpful thanks a lot!!

Go Up