Need help for program

hi,

first, sorry for my bad english i'm french. i work with an arduino uno with ethernet shield for a domotic project. to explain shortly,my programm read 2 analogic sensor and log values periodicaly on sd card. Also, periodicaly, i send the log on a server.until no problem. But when i have send the log i want to save it in another file who store all data and finally remove it.
copy is not a problem but when i put this line SD.remove(logname); compilation is ok but transfer to arduino produce an error :

avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x64
avrdude: stk500_cmd(): programmer is out of sync

i don't understand...

thanks for help.

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


byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

int serverPort=2000;
int second = 0;
const int macLen = 18;
char maca[macLen];
String mdp;
byte ipserver[4];
String mode;
int cptMin=0;
const byte BUFFER_SIZE = 32;

/* User variables */
int idclient = 0, serverip = 0, port = 0;
/* Declare global buffer and pair pointers */
char buffer[BUFFER_SIZE], *key, *value;
/* Declare iterator, buffer lenght and line counter */
byte i, buffer_lenght, line_counter = 0;

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to
char logname[]="datalog.txt";
EthernetClient client;
File file;
int sensorPin = A5;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor


void setup() 
{
  // start the serial for debugging
  Serial.begin(9600);
  
  
  
    //initialisation partie sd
   Serial.print(F("Initializing SD card..."));
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println(F("Card failed, or not present"));
    // don't do anything more:
    return;
  }
  Serial.println(F("card initialized."));
 
 
 
  //chargement du ficheir de config depuis la sd et parsage des variables
  File myFile = SD.open("config.txt");
  while(myFile.available() > 0 ){
  int i = 0;
  while((buffer[i++] = myFile.read()) != '\n') 
  {
      if(i == BUFFER_SIZE) 
      {    
        while(myFile.read() != '\n');
        break; // Stop buffering
      }
  } 
  buffer_lenght = i;
  if(i == BUFFER_SIZE) 
  {
      Serial.print(F("Line overflow at line "));
      Serial.println(line_counter, DEC);
  }
  buffer[--i] = '\0';
  ++line_counter;
  if(buffer[0] == '\0' || buffer[0] == '#') continue;
  i = 0;
  while(buffer[i] == ' ' || buffer[i] == '\t')
  {
      if(++i == buffer_lenght) break; // Skip lines with just tabs and spaces
  }
  if(i == buffer_lenght) continue; // Handle lines with just tabs and spaces
  key = &buffer[i];
  while(buffer[i] != '=')
  {
     if(buffer[i] == ' ' || buffer[i] == '\t') buffer[i] = '\0';
        
      if(++i == buffer_lenght) {
        Serial.print(F("Parsing separator error at line "));
        Serial.println(line_counter, DEC);
        break; // Skip malformed lines
      }
    }
    if(i == buffer_lenght) continue; // Handle malformed lines
    buffer[i++] = '\0';   
    while(buffer[i] == ' ' || buffer[i] == '\t') 
    {
      if(++i == buffer_lenght) 
      {
        Serial.print(F("Parsing value error at line "));
        Serial.println(line_counter, DEC);
        break; // Skip malformed lines
      }
    }
    if(i == buffer_lenght) continue; // Handle malformed lines
    value = &buffer[i];

   
    if(strcmp(key, "idclient") == 0) 
    {
      idclient = atoi(value);
    } 
    else if(strcmp(key, "mdp") == 0)
    {
      mdp = value;
    }
    else if(strcmp(key, "serverip") == 0)
    {
      int i=0;     
      char *str;
        while ((str = strtok_r(value, ".", &value)) != NULL) // delimiter is the semicolon
        {
          ipserver[i]=atoi(str);
          i++;
        }
      
  
    } 
    else if(strcmp(key, "port") == 0)
    {
      port = atoi(value);
    } 
    else if(strcmp(key, "mode") == 0)
    {
      mode = value;
    } 
    else 
    { // Default 
      Serial.print(F("parametre inconnu "));
      Serial.println(key);
    }

  }
  myFile.close();// fermeture du fichier de conf apres son chargement

 Serial.print(F("chargement de la config OK pour le client "));
 Serial.println(idclient);

  
  initConnexion();
 
  
}
void loop()
{
  pulse();
  if(second==1)
  {
      sensorValue = analogRead(sensorPin); 
      File dataFile = SD.open(logname, FILE_WRITE);
      // if the file is available, write to it:
      if (dataFile) 
      {
          dataFile.println(""+sensorValue);
          dataFile.close();    
      }  
      else 
      {
          Serial.println(F("error opening datalog.txt"));
      }      
      
      
      cptMin++;
      if(cptMin==20)// toute les 60s envoie du fichier data
      {
         
         if(connexion()==1);//etablie la connexion et envoie hello au server
         {
           delay(1000);//attente avant de lire ce quon recoit
           String cmdRecu="";
           while (client.available()) 
           {
               cmdRecu=cmdRecu+(char)client.read();                      
           }
           //Serial.print(cmdRecu);
           if(cmdRecu=="crownserver")//ok il s'agit bien du server crown
           {
              Serial.println(F("server crown contacte avec succes")); 
              client.print("transfert:");
              client.print(idclient);
              client.print(":");
              client.println(mdp);
              delay(1000);//attente avant de lire ce quon recoit
              cmdRecu="";
              while (client.available()) 
              {
               cmdRecu=cmdRecu+(char)client.read();                      
              }
             if(cmdRecu=="transfertOk")//ok il s'agit bien du server crown
             {
               Serial.println(F("authentification ok debut du transfert"));
               cptMin=0;    
              File dataFile = SD.open(logname);
              while(dataFile.available())
              {
              
                 client.print((char)dataFile.read());
              
              }            
              dataFile.close();
              client.println(F("fintransfert"));
              delay(1000);//attente avant de lire ce quon recoit
              cmdRecu="";
              while (client.available()) 
              {
               cmdRecu=cmdRecu+(char)client.read();                      
              }
              if(cmdRecu=="transfertAck")//ack de la part du server comme quoi les relevé ont bien ete envoye
              {
                Serial.println(F("transfert reussi"));
                deconnexion();//on deconnecte
                log();
              }
             }//fin if transfert accepté
           }//fin if ok server crown
         }//fin if connexion
         cptMin=0;  
       
        
      }//fin if cptmin
  }//fin if second
  if(Serial.available())
  {
    char c = Serial.read();
    client.println(c);
  }
  
}
  




int connexion()
{
   IPAddress serverIP(ipserver[0],ipserver[1],ipserver[2],ipserver[3]); // IP Adress to our Server
  // if you get a connection to the Server
      Serial.print(F("connecting to Crown Server "));
  Serial.print(serverIP);
  Serial.print(":");
  Serial.print(serverPort);
  Serial.println(F("..."));
  
  if (client.connect(serverIP, serverPort)) {
    Serial.println(F("connected"));//report it to the Serial   
    Serial.println(F("sending Message : Hello"));//log to serial
    client.println(F("Hello"));//send the message
    return 1;
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println(F("connection failed"));
    return 0;
  }  
  
}


int deconnexion()
{
  //Ethernet.stop();
  client.stop();
   Serial.println(F("disconnected"));
}

int initConnexion()
{
  
 IPAddress serverIP(ipserver[0],ipserver[1],ipserver[2],ipserver[3]); // IP Adress to our Server
  
  Serial.print(F("demarrage de l'interface TCP en mode "));
  Serial.print(mode);
  Serial.print(F("..."));
  

  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println(F("Failed to configure Ethernet using DHCP"));
    return 0;
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("OK");
  return 1;  
}


void log()
{
  
 File dataFile = SD.open(logname,FILE_WRITE);
  
  File storeFile= SD.open("/LOGS/log.txt", FILE_WRITE);
  Serial.println(F("debut sauvegarde log sur sd"));       
  while(dataFile.available())
  {
   storeFile.print((char)dataFile.read());
   //dataFile.print((char)0x08);
  } 
  Serial.println(F("fin sauvegarde log sur sd"));     
   dataFile.close();
  storeFile.close();
  SD.remove(logname);
 //dataFile = SD.open(logname,FILE_WRITE);
   
 
}

When I try to verify your code I get:

sketch_sep20a.cpp: In function 'void loop()':
sketch_sep20a:157: error: 'pulse' was not declared in this scope

When I comment out that line it compiles and uploads to my Arduino UNO R2 just fine.

Try a different USB cable or port.

i have already tried... i try to replace SD lib by SdFat lib to see if it's better

it works with sdfat lib. thanks