i cant open my files on Sd card to write to it?

my project uses a arduino Uno with xbee , a memory card and a RTC DS1307, when my code is running the files are not opening and when I remove the RTC +5v pin I can open the file and write into it
,cant we connect three serial devices to arduino please help me, the below is my code

#include <SPI.h>
#include<SD.h>
#include<SoftwareSerial.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;
File file;
int CS_PIN=10;
SoftwareSerial chat(2,3);    //Rx,Tx

void setup() 
{
Serial.begin(9600);
chat.begin(9600);
delay(1000);
initializeSD();
  delay(1000); 
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(2015,6,2,9,31,0));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
      
  delay(1000); 
}
void loop() 
{
 int x;

DateTime now = rtc.now();
  delay(1000);
 createFile("stack.txt");
]
   if (chat.available()>0)
 {  

  while (chat.available()>0)
    {
    x=chat.read();
     Serial.println(x);
     delay(1000);
    
  
  switch(x)
      {
        case 49:
        {
Serial.println(file);
  // if the file is available, write to it:
  if (file==0) {
    file.println("  Mrs.K.HARIPRIYA   EMBEDED SYSTEMS  ");
    
   
    file.close();
    // print to the serial port too:
    Serial.println("  Mrs.K.HARIPRIYA   EMBEDED SYSTEMS  ");
  }
  break;
      }
        case 50:
        {

  // if the file is available, write to it:
  if (file) {
    file.println(" Mrs.N.Parijatham  CONTROL SYSTEMS");
    
   
    file.close();
    // print to the serial port too:
    Serial.println(" Mrs.N.Parijatham  CONTROL SYSTEMS");

  }
  break;
      }
        case 51:
        {

  // if the file is available, write to it:
  if (file) {
    file.println("  Mr.K.Praveen      DSP  ");
    file.close();
    // print to the serial port too:
    Serial.println("  Mr.K.Praveen      DSP  ");
  }
  break;
      }
        case 52:
        {

  // if the file is available, write to it:
  if (file) {
    file.println("  Mr.TSRCH Murthy   DC  ");
    
    file.close();
    // print to the serial port too:
    Serial.println("  Mr.TSRCH Murthy   DC  ");
  }
  break;
      }
        case 53:
        {

  // if the file is available, write to it:
  if (file) {
    file.println("  Mr.BALA KRISHNA   OOPS  ");

    file.close();
    // print to the serial port too:
    Serial.println("  Mr.BALA KRISHNA   OOPS  ");
  }
  break;
      }
        case 73:
        {
           

Serial.println(file);
  // if the file is available, write to it:
  if (file) {   Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
     file.print(now.year(), DEC);
     file.print('/');
     file.print(now.month(), DEC);
     file.print('/');
     file.print(now.day(), DEC);
     file.print(' ');
     file.print(now.hour(), DEC);
     file.print(':');
     file.print(now.minute(), DEC);
     file.print(':');
     file.print(now.second(), DEC);
     file.println();
    file.close();
    // print to the serial port too:

  }
  break;
        }
    case 69:
        {
          
  // if the file is available, write to it:
  if (file) {
   
     file.print(now.hour(), DEC);
     file.print(':');
     file.print(now.minute(), DEC);
     file.print(':');
     file.print(now.second(), DEC);
     file.println();
    file.close();
    // print to the serial port too:



  }
  break;
        }
    }
         
}
}}
      
      
          

void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

How are you powering your system? USB?

It sounds like the supply is inadequate, but when you disconnect the RTC it can supply enough for the xbee and sd card module.

Do you have a regulated external supply you can use with the barrel connector?

yes i used rps for the rtc but theres no use even then i cant print anything to sd card

I don't have an xbee to test with, but I did set up one Arduino sending software serial to another Arduino which was running your code and I found a few problems, but after fixing them I could read data input from software serial, read the rtc, and write to the sd card.

Your program design, which opens the file at the start of the loop and closes it within the switch case statements causes problems after a few cycles with no input. I had to add a file.close() statement at the end of the loop to keep the program running. I don't know about the ultimate use of your program, but you might find that modifying the structure to read the data, switch case, and then open and close the file in each switch case arguments might serve you better.

There were two other simple corrections. There is a stray character at line 44 which keeps the sketch from compiling, and the if (file == 0) at line 61 is wrong, and should be if(file).

#include <SPI.h>
#include<SD.h>
#include<SoftwareSerial.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;
File file;
int CS_PIN = 10;
SoftwareSerial chat(2, 3);   //Rx,Tx

void setup()
{
  Serial.begin(9600);
  chat.begin(9600);
  delay(1000);
  initializeSD();
  delay(1000);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
  // following line sets the RTC to the date & time this sketch was compiled
  //rtc.adjust(DateTime(2015, 6, 2, 9, 31, 0));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

  delay(1000);
}
void loop()
{
  int x;

  DateTime now = rtc.now();
  delay(1000);
  createFile("stack.txt");
  //] //won't compile with stray character
  if (chat.available() > 0)
  {

    while (chat.available() > 0)
    {
      x = chat.read();
      Serial.println(x);
      delay(1000);


      switch (x)
      {
        case 49:
          {
            Serial.println(file);
            // if the file is available, write to it:
            //if (file == 0) {
            if (file){
            file.println("  Mrs.K.HARIPRIYA   EMBEDED SYSTEMS  ");


            file.close();
            // print to the serial port too:
            Serial.println("  Mrs.K.HARIPRIYA   EMBEDED SYSTEMS  ");
          }
          break;
      }
    case 50:
      {

        // if the file is available, write to it:
        if (file) {
          file.println(" Mrs.N.Parijatham  CONTROL SYSTEMS");


          file.close();
          // print to the serial port too:
          Serial.println(" Mrs.N.Parijatham  CONTROL SYSTEMS");

        }
        break;
      }
    case 51:
      {

        // if the file is available, write to it:
        if (file) {
          file.println("  Mr.K.Praveen      DSP  ");
          file.close();
          // print to the serial port too:
          Serial.println("  Mr.K.Praveen      DSP  ");
        }
        break;
      }
    case 52:
      {

        // if the file is available, write to it:
        if (file) {
          file.println("  Mr.TSRCH Murthy   DC  ");

          file.close();
          // print to the serial port too:
          Serial.println("  Mr.TSRCH Murthy   DC  ");
        }
        break;
      }
    case 53:
      {

        // if the file is available, write to it:
        if (file) {
          file.println("  Mr.BALA KRISHNA   OOPS  ");

          file.close();
          // print to the serial port too:
          Serial.println("  Mr.BALA KRISHNA   OOPS  ");
        }
        break;
      }
    case 73:
      {


        Serial.println(file);
        // if the file is available, write to it:
        if (file) {
          Serial.print(now.year(), DEC);
          Serial.print('/');
          Serial.print(now.month(), DEC);
          Serial.print('/');
          Serial.print(now.day(), DEC);
          Serial.print(' ');
          Serial.print(now.hour(), DEC);
          Serial.print(':');
          Serial.print(now.minute(), DEC);
          Serial.print(':');
          Serial.print(now.second(), DEC);
          Serial.println();
          file.print(now.year(), DEC);
          file.print('/');
          file.print(now.month(), DEC);
          file.print('/');
          file.print(now.day(), DEC);
          file.print(' ');
          file.print(now.hour(), DEC);
          file.print(':');
          file.print(now.minute(), DEC);
          file.print(':');
          file.print(now.second(), DEC);
          file.println();
          file.close();
          // print to the serial port too:

        }
        break;
      }
    case 69:
      {

        // if the file is available, write to it:
        if (file) {

          file.print(now.hour(), DEC);
          file.print(':');
          file.print(now.minute(), DEC);
          file.print(':');
          file.print(now.second(), DEC);
          file.println();
          file.close();
          // print to the serial port too:



        }
        break;
      }
    }

  }
}
file.close(); //Prevents file open error when no input
}




void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

int createFile(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    Serial.println("File created successfully.");
    return 1;
  } else
  {
    Serial.println("Error while creating file.");
    return 0;
  }
}

i tried giving Rps to the RTC but the RTC is not working with the RPS and when i tried giving RPS to memory card the memorycard is initialized but file is not created , i fixed errors posted by you, thank you.

Do you have each component (xbee, rtc, sd module) working alone?

Can you use the RTC and SD together without the xbee and software serial? Use input from the serial monitor to the uno.

yes i checked i can use SD and RTC together without the xbee, but i need input the value using a keypad which is in a different room so i used xbee.

Does the XBEE work with the UNO and a serial monitor and no RTC or SD? Do yo have the RX/TX pins correct?

Does your program run if you use the XBEE and the RTC together without the SD card and only Serial output to the monitor?

Does your program run if you use the XBEE and SD card without the RTC?

There are only a few combinations of 2 of your three elements, and if the code works for each pair, I'd recommend looking again at the power supply issues.

As I said, I can get your code to run without the XBEE but with software serial input to pins 2 and 3.

yes i checked withe the combinations of 2 there ar three combination and they are absolutely working i tried giving a 5v external supply to the RTC, but that did not work and i also tried making a pin high and using it as voltage,when i used external supply for sd card and thge rtc and xbee powered by uno then sd card is initialized but the file is not created, even if the file is created nothing is written into the file..

when i used external supply for sd card and thge rtc and xbee powered by uno

When you use the external supply, are you connecting the grounds?