editing ftp code to write the file and send it

hi everyone

i am trying to edit the code in Arduino Playground - FTP
because this code is sending empty files to the server

i need to write random string in it before sending it
first i tried using any line, write it in the txt file then send it to the ftp server, but looks like the file always uploaded empty

i took the SD card and out it in my laptop, and i could see the text in it, but when the arduino writes it and send it, it get sent empty

if i used the file without trying to write in it, the file is sent successfully , but when i try to write it using the code it doesnt work
the code after editing is

char fileName[13] = "create.txt";
int s=1;
void setup()
{
  Serial.begin(9600);

  digitalWrite(4,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }
  

  Ethernet.begin(mac, ip); 
  digitalWrite(4,HIGH);
  delay(2000);
  Serial.println(F("Ready. Press f or r"));
}

void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(s == 1)
  {
    if(doFTP()) {Serial.println(F("FTP OK"));
    s++;
}
    else Serial.println(F("FTP FAIL"));
  }

  if(inChar == 'r')
  {
    readSD();    
  }

}

File fh;


byte doFTP()
{
#ifdef FTPWRITE
    
  fh = SD.open(fileName,FILE_WRITE);
 
#else
  
  fh = SD.open(fileName,FILE_WRITE);
 
#endif

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return 0;    
  }

#ifndef FTPWRITE  
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;    
  }
#endif

  Serial.println(F("SD opened"));
 fh = SD.open(fileName,FILE_WRITE);
   fh.println("*****random*****");
fh.close();
  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER admin"));

  if(!eRcv()) return 0;

  client.println(F("PASS 1122"));

  if(!eRcv()) return 0;

  client.println(F("SYST"));

  if(!eRcv()) return 0;

  client.println(F("Type I"));

  if(!eRcv()) return 0;

  client.println(F("PASV"));

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));    

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(server,hiPort)) {
    Serial.println(F("Data connected"));
  } 
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }

#ifdef FTPWRITE 
  client.print(F("STOR "));
  client.println(fileName);
#else
  client.print(F("RETR "));
  client.println(fileName);
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println(F("Writing"));
   

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);      
      Serial.write(c); 
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));

  if(!eRcv()) return 0;

  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("SD closed"));
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}


void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("SD closed"));
}

void readSD()
{
  fh = SD.open(fileName,FILE_READ);

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return;    
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

i only added the fh.println()

please help

I really don't understand what you are doing.

#ifdef FTPWRITE
   
  fh = SD.open(fileName,FILE_WRITE);
 
#else
 
  fh = SD.open(fileName,FILE_WRITE);
 
#endif

If the name FTPWRITE is defined, open a file for write. If it isn't defined, open the same file, the same way. What is the purpose of the conditionals?

 fh = SD.open(fileName,FILE_WRITE);
   fh.println("*****random*****");
fh.close();

Unconditionally open the file that is already open. Write the string containing random, which is anything but random, and then close the file.

The fine folks at http://snippets-r-us.com might be able to help you with your snippets.

PaulS:
I really don't understand what you are doing.

#ifdef FTPWRITE

fh = SD.open(fileName,FILE_WRITE);

#else

fh = SD.open(fileName,FILE_WRITE);

#endif



If the name FTPWRITE is defined, open a file for write. If it isn't defined, open the same file, the same way. What is the purpose of the conditionals?



fh = SD.open(fileName,FILE_WRITE);
  fh.println("random");
fh.close();



Unconditionally open the file that is already open. Write the string containing random, which is anything but random, and then close the file.

The fine folks at http://snippets-r-us.com might be able to help you with your snippets.

thank you for your reply

i found fixed the problem by retrying to edit the code