Trying to understand SD card access

Hey All,

(apologies i have been reading around but am not understanding what i am reading.)

Basically trying to read/write information to/from a SD card for settings storage.

The idea is to have a multi line comma deliminated file with the settings obviously stored between each comma.

Seems to be a pretty common issue that people can read/write to the cards but when reading are only able to get Strings via Serial.Write(int) which converts the sector information to characters to then be displayed via the Serial connection. (hoping i am right here)

I have tried loading this information directly into a string by basically adding all of the chars to string then using String(variable). This seems to return a bunch of numbers that i can do anything with.

Now from reading around it seems the information is loaded into "Sectors" each sector contains 2 chars. I am starting to think that i need to create a Byte Array with 2 sectors. This would be used to temporaryaly store the a sector convert it to the appriopriate Char for a string and add it onto the end of a string and repeat. Now am i right in this thinking? If this is not clear i have put some notes in the code below to explain where i feel i need to do this(notes are in the read loop).

Does anyone know of any SDfat library's that have predefined file.ReadLine (as a string) functions like good old easy pc coding :D.

Sorry if what i have said makes no sense i am trying to get my head around it :smiley:

/*
 * Read logfile - demo of pathnames and current working directory
 */
#include <SdFat.h>

// file system object
SdFat sd;

SdFile file;
//------------------------------------------------------------------------------
void setup() {
  int c;
  Serial.begin(9600);

  // Initialize SdFat - print detailed message for failure
  if (!sd.init()) sd.initErrorHalt();

  // set current working directory
  if (!sd.chdir("LOGS/2011/JAN/")) {
    sd.errorHalt("chdir failed. Did you run eventlog.pde?");
  }
  // open file in current working directory
  if (!file.open("LOGFILE.TXT", O_READ)) sd.errorHalt("open failed");

  // copy the file to Serial
  while ((c = file.read()) >= 0) 
  {

// A 2 setor array would have been previously defined. 
// First loop would write to first sector of array.
// Second loop would write to second sector of array. It would then convert/store this into a string.
// Repeat from First loop step
// i beleive the converstion would be somethign like String stringOne =  String(bytearray[0] + bytearray[1] ,DEC);   
     Serial.write(c);
  }


  Serial.println();
  Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}

Give this library a shot, may have stuff that supports what you want better.

http://code.google.com/p/sdfatlib/

LOL just found that site and the solution to my problem only to come here and see that link :smiley:

Thank you very much for the link it would have resolve my problems if i did not already locate it :smiley:

I have noted the code below it seems declaring C as a char instead of an int allows it to be parsed to the string with ease :smiley:

/*
 * Read logfile - demo of pathnames and current working directory
 */
#include <SdFat.h>

// file system object
SdFat sd;

SdFile file;
//------------------------------------------------------------------------------
void setup() {
  char c = 0;
  Serial.begin(9600);
  String inString = String();  // String to hold the file before passing to program
  
  // Initialize SdFat - print detailed message for failure
  if (!sd.init()) sd.initErrorHalt();

  // set current working directory
  if (!sd.chdir("ALARMS")) {
    sd.errorHalt("chdir failed. Did you run eventlog.pde?");
  }
  // open file in current working directory
  if (!file.open("ALARMS.TXT", O_READ)) sd.errorHalt("open failed");

  // copy the file to Serial
  while ((c = file.read()) >= 0){
    inString += c;
  }
  Serial.print(inString);
  Serial.println("Done");
}
//------------------------------------------------------------------------------
void loop() {}