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);
}
}
當我將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
@BryanS。您只需使用'in'標誌快速打開文件以查看它是否存在,然後在檢查後立即關閉它。然後你用普通的「out」(或「out」和「in」)模式打開它。 – 2012-03-21 09:19:57
但是當我編寫testStream.open(fileName.c_str(),iOS :: in),並且將testStream作爲ifstream變量時,如果(testStream.good())則寫入文件,然後清除並關閉文件流,否則bool = true並清除並關閉文件流是不是你說我應該做的那件事?當我用instream打開它的模式時,它全部搞砸了 – 2012-03-21 09:28:38