Pages: [1]   Go Down
Author Topic: Reading .txt files from SD card  (Read 1470 times)
0 Members and 1 Guest are viewing this topic.
GREECE
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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...
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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';
return(num);

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

GREECE
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That was very helpful thanks a lot!!
Logged

Pages: [1]   Go Up
Jump to: