Files are created but I can't write data to them

Hello Everyone,
I am using an Arduino UNO R3 and an Adafruit GPS ultimate logger shield in my project. I am able to create files on the SD card but I do not seem to be able to write any data into those files.

The hardware setup is sound and I have used some of their sample code to successfully write some data to a file on the SD card a few times (their code has worked fine). However, I need to modify it to allow me to write different data sets into separate files on the SD card. This is where I began to expierece the difficulties I described above. The code below will create a file on the SD card but they are always empty. Any help would be greatly appreciated. Please let me know if I need to clarify anything. Thanks in advance.

#include <SPI.h>
#include <SoftwareSerial.h> 
#include <SD.h>


SoftwareSerial mySerial(8, 7);

char filename[15];
char filename1[15];

// Set the pins used
#define chipSelect 10
#define ledPin 13

File logfile; 
File logfile1;

void setup() 
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT); 
  pinMode(10, OUTPUT); 

  
    if (!SD.begin(chipSelect)) 
    {  
      Serial.println("Card init. failed!");
    }
    
  strcpy(filename, "TXTFIL00.TXT"); 
  for (uint8_t i = 0; i < 100; i++) 
    {
      filename[6] = '0' + i/10; 
      filename[7] = '0' + i%10; 
      
      if (! SD.exists(filename)) 
      {
        break; 
      }
  }



  logfile = SD.open(filename, FILE_WRITE);
  if( ! logfile ) 
    {
      Serial.print("Couldnt create "); 
      Serial.println(filename);
    }
   
  Serial.print("Created ");
  Serial.println(filename);
  logfile.println("Test\n");
  

// Initialize a second File
  
  
  strcpy(filename1, "TXTFIL00.TXT");
  for (uint8_t i = 0; i < 100; i++) 
    {
      filename1[6] = '0' + i/10; 
      filename1[7] = '0' + i%10; 
      
      if (! SD.exists(filename1)) 
      {
        break;
      }
  }


  logfile1 = SD.open(filename1, FILE_WRITE); 
  if( ! logfile1 ) 
    {
      Serial.print("Couldnt create "); 
      Serial.println(filename1);
    }


  Serial.print("Created "); 
  Serial.println(filename1);
  logfile1.println("Test\n");

  Serial.println("Setup Complete"); //Signifies the end of the setup function
}



void loop()
{
 
    char *firststring = "First String";
    char *secondstring = "second string";


  Serial.print("Writing to "); //This appears in the serial monitor when the program runs (once at the beginning) JB
  Serial.println(filename);

// write to the first file
    

    uint8_t stringsize = strlen(firststring);
    if (stringsize != logfile.write((uint8_t *)firststring, stringsize))
    
    
    logfile.flush();
    logfile.write("\n");
    Serial.println();
    logfile.println("firststring Please\n");
   

  Serial.print("Writing to "); 
  Serial.println(filename1);

// write to the 2nd file

   uint8_t stringsize1 = strlen(secondstring); 
    if (stringsize1 != logfile1.write((uint8_t *)firststring, stringsize1))   
    logfile1.flush(); 
    logfile1.write("\n");
    Serial.println();
    logfile1.println("firststring Please\n");
    
    
  }


/* End code */

Are the files opened to be written to?

Gerihatrick:
Are the files opened to be written to?

I was under the impression that the following line was the doing that but I may be wrong...

logfile = SD.open(filename, FILE_WRITE);

Thanks

bshmstr:
Hello Everyone,
I am using an Arduino UNO R3 and an Adafruit GPS ultimate logger shield in my project. I am able to create files on the SD card but I do not seem to be able to write any data into those files.

The hardware setup is sound and I have used some of their sample code to successfully write some data to a file on the SD card a few times (their code has worked fine). However, I need to modify it to allow me to write different data sets into separate files on the SD card. This is where I began to expierece the difficulties I described above. The code below will create a file on the SD card but they are always empty. Any help would be greatly appreciated. Please let me know if I need to clarify anything. Thanks in advance.

#include <SPI.h>

#include <SoftwareSerial.h>
#include <SD.h>

SoftwareSerial mySerial(8, 7);

char filename[15];
char filename1[15];

// Set the pins used
#define chipSelect 10
#define ledPin 13

File logfile;
File logfile1;

void setup()
{
 Serial.begin(115200);
 pinMode(ledPin, OUTPUT);
 pinMode(10, OUTPUT);

if (!SD.begin(chipSelect))
   {  
     Serial.println("Card init. failed!");
   }
   
 strcpy(filename, "TXTFIL00.TXT");
 for (uint8_t i = 0; i < 100; i++)
   {
     filename[6] = '0' + i/10;
     filename[7] = '0' + i%10;
     
     if (! SD.exists(filename))
     {
       break;
     }
 }

logfile = SD.open(filename, FILE_WRITE);
 if( ! logfile )
   {
     Serial.print("Couldnt create ");
     Serial.println(filename);
   }
 
 Serial.print("Created ");
 Serial.println(filename);
 logfile.println("Test\n");

// Initialize a second File
 
 
 strcpy(filename1, "TXTFIL00.TXT");
 for (uint8_t i = 0; i < 100; i++)
   {
     filename1[6] = '0' + i/10;
     filename1[7] = '0' + i%10;
     
     if (! SD.exists(filename1))
     {
       break;
     }
 }

logfile1 = SD.open(filename1, FILE_WRITE);
 if( ! logfile1 )
   {
     Serial.print("Couldnt create ");
     Serial.println(filename1);
   }

Serial.print("Created ");
 Serial.println(filename1);
 logfile1.println("Test\n");

Serial.println("Setup Complete"); //Signifies the end of the setup function
}

void loop()
{

char *firststring = "First String";
   char *secondstring = "second string";

Serial.print("Writing to "); //This appears in the serial monitor when the program runs (once at the beginning) JB
 Serial.println(filename);

// write to the first file

uint8_t stringsize = strlen(firststring);
   if (stringsize != logfile.write((uint8_t *)firststring, stringsize))
   
   
   logfile.flush();
   logfile.write("\n");
   Serial.println();
   logfile.println("firststring Please\n");

Serial.print("Writing to ");
 Serial.println(filename1);

// write to the 2nd file

uint8_t stringsize1 = strlen(secondstring);
   if (stringsize1 != logfile1.write((uint8_t *)firststring, stringsize1))  
   logfile1.flush();
   logfile1.write("\n");
   Serial.println();
   logfile1.println("firststring Please\n");
   
   
 }

/* End code */

why don't you close the files?

The file.write() just adds the data to an internal buffer, until 512 bytes have been written to the buffer NOTHING is written to the file. You must Close the file.

Closing the file flushes any remaining data from the buffer to the SDCARD, Updates the directory file size field.

The behavior you are seeing is consistent with your program:

  • You create a file for write.
  • You never CLOSE the file, (which causes the library to update the directory record for this file.
  • This results in a file size of 0.

Also, you have this code:

  if (stringsize != logfile.write((uint8_t *)firststring, stringsize))
   
   
    logfile.flush();

Why do you only flush() the file if the write() fails?

Chuck.