2012-03-21 28 views
0

私は、ofstreamファイルが存在するかどうかを確認することと少し混乱しています。 Macの10.5.8用のXcodeで、私はそれを開いてコマンドを過ぎて行く一度 XcodeでC++を開かずにファイルが存在するかどうかを確認してください。

testStream.open(fileName.c_str()); 
    if (testStream.good()) { 
      cout << "The file already exists, please choose another" 
      << endl; 
      testStream.clear(); 
      testStream.close(); 
    } 

しかし、Xcodeの4の

が、それは空のファイルを作成し、書き込むことができたので、if文は常にトリガされます。ファイルが存在するかどうかを確認する良い方法はありますか?私はそれを変更しようとしている:

testStream.open(fileName.c_str(), iOS::out | iOS::nocreate); 

しかし、コンパイラはNOCREATEという名前のメンバーが存在しないというエラーを取得します。 お願いしますか?

それが助け場合は、ここでは全体のコンパイルコード(私のプログラムは、シーザーCYPHERである)である:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <sstream> 
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->clear(); 
    inFile->close(); 
    outFile = openOutFile(); 
    printSentence(outData, outFile); 
    outFile->clear(); 
    outFile->close(); 
    return 0; 
} 
// Input Functions 
string getInput() { 
    cout << "Enter an input file name: "; 
    string inFileName; 
    getline(cin, inFileName); 
    return inFileName; 
} 
string getOutput() { 
    string outFileName; 
    cout << "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->open(inFileName.c_str()); 
     if (inFile->fail()) { 
      cout << "Couldn't open file" << 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 << "The file already exists, please choose another" 
      << endl; 
      testStream.clear(); 
      testStream.close(); 
     } 
    else { 
      isUnique = true; 
      testStream.clear(); 
      testStream.close(); 
    } 
} 
while (!isUnique); 
outFile = new ofstream; 
outFile->open(fileName.c_str()); 
return outFile; 
} 
int getShiftValue() { 
    int shiftNum; 
    string trash; 
    cout << "Please enter shift value: "; 
    cin >> 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 <= inData.length() - 1; idx++) { 
      if (inData[idx] >= 'a' && inData[idx] <= 'z') { 
       outChar = (((inData[idx] - 'a') + shiftNum) % 26) + 
       'a'; 
       outSentence << outChar; 
      } 
      else if (inData[idx] >= 'A' && inData[idx] <= 'Z') { 
       outChar = (((inData[idx] - 'A') + shiftNum) % 26) + 
       'A'; 
       outSentence << outChar; 
      } 
      else { 
       outChar = inData[idx]; 
       outSentence << outChar; 
     } 
    } 
} 
    outSentence >> outData; 
    return outData; 
} 
// Output funcitons 
void menu() { 
    cout << "C A E S A R C Y P H E R P R O G R A M" << endl 
    << "========================================" << endl; 
    return; 
} 
void printSentence (string outData, ofstream * outFile) { 
    int idx = 0; 
    char outChar; 
    stringstream outString; 
    outString << outData; 
    for (idx = 0; idx <= outData.length() - 1; idx++) { 
     outChar = outString.get(); 
     outFile->put(outChar); 
    } 
} 

答えて

0

は、あなたはそれが存在するかどうかを確認するために最初の入力モード(モードios_base::in)でファイルを開くことができます。または、システムコールstatを使用します。

+0

testStream.open(fileName.c_str())をtestStream.open(fileName.c_str()、ios_base :: in)に変更すると、正しいファイルチェックが行われますが、ファイルは無限ループのゴミ私はプログラムを停止するまで。 iOS_base ::とは何ですか?プログラムの残りの部分に何をしていますか?私はtestStreamにifstream変数を作り、testStream(outFile)の後にofstreamをoutFile.open(fileName.c_str、iOS_base :: out)に設定しようとしました。 @BryanS。 –

+0

'in'フラグを使ってすぐにファイルを開いてファイルが存在するかどうかを確認し、チェックの直後にファイルを閉じます。次に、通常の 'out'(または' out'と 'in')モードで開きます。 –

+0

しかし、testStream.open(fileName.c_str()、iOS :: in)をifstream変数であるtestStreamで記述し、if(testStream.good())が存在し、ファイルストリームをクリアして閉じ、else bool = trueで、ファイルストリームをクリアして閉じても、私がやるべきことと同じことではありませんか?そして私がoutstreamでoutstreamで開くときに、それはすべてがねじ込まれて来た –

関連する問題