2012-03-21 53 views
0

我有点困惑,检查是否存在tostream文件写入它。在Xcode中为Mac 10.5.8,我能写检查文件是否存在,而不打开它在Xcode 4上的C++

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

但在Xcode中4,一旦它进入过去open命令,它会创建一个空文件,所以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); – 2012-03-21 09:14:14

+0

@BryanS。您只需使用'in'标志快速打开文件以查看它是否存在,然后在检查后立即关闭它。然后你用普通的“out”(或“out”和“in”)模式打开它。 – 2012-03-21 09:19:57

+0

但是当我编写testStream.open(fileName.c_str(),iOS :: in),并且将testStream作为ifstream变量时,如果(testStream.good())则写入文件,然后清除并关闭文件流,否则bool = true并清除并关闭文件流是不是你说我应该做的那件事?当我用instream打开它的模式时,它全部搞砸了 – 2012-03-21 09:28:38

相关问题