SD card shield <FAT.h> library help

I'm using an ebay bought SD card shield with my arduino - http://bit.ly/9FKjRb

The example doc that came with the library zip gives the very basic functions to write to the SD card. Is there somewhere where I can see all the functions included in this library?

Specifically I want to know how to write a new line. Right now I'm using the filename.write(“hello world”); to put all my data on the SD card. How do I write a new line to organize my data better? (\n doesn't seem to work, but maybe I'm doing it wrong?)

Thank you for any help you can give!!!

You installed the library somewhere. Look in the directory where you installed the library. There are a bunch of .cpp and .h files. They are text files, so open them with you favorite editor.

The code to actually write to a file on the card is typically based on the Print class, which includes a println() function that adds a new line (which actually consists of a carriage return and a line feed).

Some editors interpret the carriage return only (\n) as a new line. Others expect both a carriage return and a new line (\r), in the correct order (and, no I never remember the correct order).

I can't make heads or tails of what's in the library files. Plus there are a bunch of them. I tried to scan through but it's way over my newbie head... Would anyone be able to help? Here is the library as a zip -

The problem with this code is that the info for it is in the actual header file. You can open the header file with WordPad, and go to the function that you want to look at by searching using ctrl+f.

To enter a new line, just put \n in your [edit]code string[/edit]. It doesn't print in Notepad, but it works in WordPad.

It is also a problem for people who don't have a lot of coding experience, as it isn't an object based class (which people who have solely used the Arduino are comfortable with)

(I know how you feel about posting code, PaulS, but this is quite a problem)

I found problems with the examples, and have came up with my own subroutines.

I was having trouble creating a few new files at once, so I added this to my create file:

void create_new_file(char *file_save_name) {

  if(!fat_create_file(dd, file_save_name, &dir_entry)){
    fat_delete_file(fs, &dir_entry);
    fat_create_file(dd, file_save_name, &dir_entry);
  }

  sd_raw_sync();

}

This is a subroutine that writes what is in buffer to your file. I was having trouble creating a default value to _offset, so I just decided to keep it as an entry and write 0 there when I don't want to offset where I am writing to (you may want to overwrite the last 2 characters: To do this, just change _offset to -2).

void AddBufferToFile(char *FileWritingTo, int32_t _offset){

  int32_t OffSet = _offset;

  //Open the file at the beginning of the file 
  file_handle = open_file_in_dir(fs, dd, FileWritingTo);

  if(file_handle)
  {

    //Now go to the end of the file to write some data.
    if(fat_seek_file(file_handle, &OffSet, FAT_SEEK_END))
    {

      //Write the new 'buffer' string to the end of the file
      fat_write_file(file_handle, (const uint8_t*)buffer, strlen(buffer));

      //sychronise SD (Required)
      sd_raw_sync();
    }

    //Close the file
    fat_close_file(file_handle);
  }

}

This next on gets info from a file (name is in a variable "char file_name[30];"

Note that you can change file_name to an entry to the function. However, I do not change the file that I am writing to, so I was just saving some code by taking the name from the same place each time.

int AddFileToBuffer(int32_t OffSet){
  int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card. 
  //Open the file      
  file_handle = open_file_in_dir(fs, dd, file_name);
  //Read up to 512 bytes from the file

    if(file_handle) {

    fat_seek_file(file_handle, &OffSet, FAT_SEEK_SET);

    bytes_read = fat_read_file(file_handle, (uint8_t*)buffer, BUFFERSIZE);

    //Close the file before moving on to the next one.
    fat_close_file(file_handle);

    return(bytes_read);
  }
}

Here is an example on how to use it. It once again uses the variable file_name as a file destination, and an input file called "_xml_file_name[]". The idea of this function is to save data in a separate file, and at the end of recording data, all the different records are put together in an XML format.

void printDataFromFile(char _xml_file_name[]) {
  int bytes_read = 0;
  int32_t OffSet = 0;

  do {
    OffSet += (int32_t)bytes_read;
    bytes_read = AddFileToBuffer(OffSet);
    buffer[bytes_read] = '\0';
    AddBufferToFile(_xml_file_name,0);
   
  } 
  while(bytes_read == BUFFERSIZE);

  strcpy_P(buffer, (char*)pgm_read_word(&(string_table[13])));
  bytes_read > 2? OffSet = -2: OffSet = 0;

  AddBufferToFile(_xml_file_name,OffSet);

}

You need to be careful that you don't omit the '\0' (EOT) character to the string you are entering.

If you have any questions, please feel free to send me a message

:slight_smile:

Wow InvalidApple thank you so much for the help! That's some awesome stuff...

I have been spending a lot of time lately going through the FAT.h library, so I'm glad to help someone.

I am trying to get though FAT.c too, but with little success. I have problems with FAT16.Write. IT DOES NOT write text at the end of the file, as it says (ok, it writes, but no more tha 512 characters).

Can somebody helps with an example? I would like to use Arduino SD shield as a logger, but it the size is limited to 512char, than it is no use.

Btw, in FAT.c is a strange code at object "fat_write_file": "uint8_t". I just can not track how it jumps back and forth.

Please, help.