title: check if file exists without opening it c++ on Xcode 4
body: <p>I am a little confused with checking to see if an ofstream file exists to write to it. On Xcode for Mac 10.5.8, I was able to write </p>
  
  <pre><code>testStream.open(fileName.c_str());
     if (testStream.good()) {
              cout &lt;&lt; "The file already exists, please choose another" 
              &lt;&lt; endl;
              testStream.clear();
              testStream.close();
      }
  </code></pre>
  
  <p>But on Xcode 4, once it goes past the open command, it creates an empty file, so the if statement will always trigger. Is there a better way to check if a file exists? I have tried changing it to: </p>
  
  <pre><code>testStream.open(fileName.c_str(), iOS::out | iOS::nocreate);
  </code></pre>
  
  <p>But the compiler gets an error that says there is no member named nocreate.
  Help Please??          </p>
  
  <p>If it helps, here is the entire compilable code (my program is a caesar cypher):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  using namespace std;
  string getInput ();
  ifstream * openInFile ();
  int getShiftValue ();
  void menu ();
  string shiftCharacters (int shiftNum, ifstream * inFile);
  string getOutput ();
  ofstream * openOutFile ();
  void printSentence (string outData, ofstream * outFile);
  int main() {
      ifstream * inFile;
      ofstream * outFile;
      string inFileName, outFileName, outData; 
      int shiftNum = 0;
      menu();
      inFile = openInFile(); 
      shiftNum = getShiftValue();
      outData = shiftCharacters(shiftNum, inFile);
      inFile-&gt;clear();
      inFile-&gt;close();
      outFile = openOutFile();
      printSentence(outData, outFile);
      outFile-&gt;clear();
      outFile-&gt;close();
      return 0;
  }
  // Input Functions
  string getInput () {
      cout &lt;&lt; "Enter an input file name: "; 
      string inFileName;
      getline(cin, inFileName); 
      return inFileName;
  }
  string getOutput () {
      string outFileName;
      cout &lt;&lt; "Enter an output file name: ";
      getline(cin, outFileName);
      return outFileName;
  }
  ifstream * openInFile () {
      ifstream * inFile;   
      bool isGood = false; 
      string inFileName;    
      inFile = new ifstream;
      do {   
          inFileName = getInput();
          inFile-&gt;open(inFileName.c_str());
         if (inFile-&gt;fail()) { 
             cout &lt;&lt; "Couldn't open file" &lt;&lt; endl;
          }
         else {
             isGood = true;
         }
      }
      while (!isGood);
     return inFile;
  }
  ofstream * openOutFile () {
      ofstream testStream; 
      ofstream * outFile;   
      bool isUnique = false; 
      string fileName;
     do {   
         fileName = getOutput();
         testStream.clear(); 
         testStream.open(fileName.c_str());
         if (testStream.good()) {
              cout &lt;&lt; "The file already exists, please choose another" 
              &lt;&lt; endl;
              testStream.clear();
              testStream.close();
          }
      else {
              isUnique = true;
              testStream.clear();
              testStream.close();
      }
  }
  while (!isUnique);
  outFile = new ofstream;
  outFile-&gt;open(fileName.c_str());
  return outFile;
  }
  int getShiftValue () {
      int shiftNum;
      string trash;
      cout &lt;&lt; "Please enter shift value: ";
      cin &gt;&gt; shiftNum;
      getline(cin, trash); 
      return shiftNum;
  }
  
  // Data manipulation functions 
   string shiftCharacters (int shiftNum, ifstream * inFile){
      string inData, outData; 
      char outChar;
      int idx = 0;
      stringstream outSentence; 
      while (getline(* inFile, inData)) {
          for (idx = 0; idx &lt;= inData.length() - 1; idx++) {
              if (inData[idx] &gt;= 'a' &amp;&amp; inData[idx] &lt;= 'z') {
                  outChar = (((inData[idx] - 'a') + shiftNum) % 26) +
                  'a';
                  outSentence &lt;&lt; outChar;
              }
              else if (inData[idx] &gt;= 'A' &amp;&amp; inData[idx] &lt;= 'Z') {
                  outChar = (((inData[idx] - 'A') + shiftNum) % 26) +
                  'A';
                  outSentence &lt;&lt; outChar;
              }
              else {
                  outChar = inData[idx];
                  outSentence &lt;&lt; outChar;
          }
      }
   }
       outSentence &gt;&gt; outData;
       return outData;
   }
  // Output funcitons
  void menu () {
      cout &lt;&lt; "C A E S A R  C Y P H E R  P R O G R A M" &lt;&lt; endl
       &lt;&lt; "========================================" &lt;&lt; endl;
     return;
  }
  void printSentence (string outData, ofstream * outFile) {
      int idx = 0;
      char outChar;
      stringstream outString;
      outString &lt;&lt; outData;
      for (idx = 0; idx &lt;= outData.length() - 1; idx++) {  
          outChar = outString.get();
          outFile-&gt;put(outChar); 
      }
  }
  </code></pre>