2012-02-07 117 views
2

修訂:這是我的整個可編譯程序。這是菜單驅動的,但我堅持的部分是選項DECRYPT,解密凱撒加密文件或數字5(可以在最初的問題中鍵入,解密可以是小寫,大寫或駱駝案件)。 ofstream變量outFile將創建一個由用戶命名的文件(必須是不存在的文件)。問題是它只會創建空文件,而不會將任何數據打印到文件中。所有變量都存儲正確的值。 cout的作品,但outFile不。有什麼我不正確的做法?我試圖測試壞,失敗和is_open,並沒有任何麻煩。我不認爲文件權限會阻止任何事情,因爲程序中的其他選項會創建並寫入文件。誰能幫我?ofstream創建一個文件,但不能寫入它

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cctype> 
#include <map> 
#include <iomanip> 
#include <vector> 
using namespace std; 
int main() { 
string inputFileName, outputFileName, inData, outData, inWord, outWord, trash; 
ifstream inFile, testStream; 
ofstream outFile; 
bool outPutOpened = false, isLowerCase = false; 
char outChar, inChar; 
int shiftNum = 0, idx = 0, total = 0, max = 0, shiftValue = 0; 
map<char,int> charMap; 
map<char,int>::iterator mapIt; 
vector<char> alphabet(26); 
vector<char>::iterator shiftIt; 
do { 
    inWord.clear(); 
    outWord.clear(); 
    cout << "Available options: " << endl; 
    cout << "1. ENCRYPT - Encrypt a file using Caesar Cypher" << endl 
     << "2. CHARFREQ - display character frequency table for file" 
     << endl << "3. Quit - Exit the program" << endl 
    << "5. DECRYPT - decrypt a cyphered file" << endl << endl; 
    cout << " Enter keyword or option index: "; 
    getline(cin, inWord); 
    outWord.resize(inWord.length()); 
    transform(inWord.begin(), inWord.end(), outWord.begin(), ::toupper); 
    if (outWord.compare("ENCRYPT") == 0 || outWord.compare("1") == 0) { 
     cout << "CAESAR CYPHER PROGRAM" << endl 
     << "======================" << endl << endl; 
     do {     
      cout << "Provide the input file name: "; 

      getline(cin, inputFileName); 

      inFile.open(inputFileName.c_str()); 
      if (inFile.fail()) { 
       cout << "Cannot open file, please try again!" << endl; 
       inFile.clear(); 
      } 
     } 
     while (!inFile.is_open()); 

     do { 
      cout << "Provide the output file name: "; 
      cin >> outputFileName; 
      getline(cin, trash);     
      testStream.clear(); 
      testStream.open(outputFileName.c_str()); 
      if(testStream.good()) { 
       cout << "That file already exists, choose another" << endl; 
       testStream.clear(); 
       testStream.close(); 
      } 
      else { 
       testStream.clear(); 
       testStream.close(); 
       outFile.open(outputFileName.c_str()); 
       if (outFile.good()) { 
        outPutOpened = true; 
       } 
      } 
     } 
     while (!outPutOpened); 
     cout << "Enter the shift number: "; 
     cin >> shiftNum; 
     getline(cin, trash); 
     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'; 
       outFile.put(outChar); 
      } 
      else if (inData[idx] >= 'A' && inData[idx] <= 'Z'){ 
       outChar = (((inData[idx] - 'A') + shiftNum) % 26) + 'A'; 
       outFile.put(outChar); 
      } 
      else { 
       outFile.put(inData[idx]); 
      } 
     } 
     } 
     inFile.clear(); 
     inFile.close(); 
     outFile.clear(); 
     outFile.close(); 
    } 
    else if (outWord.compare("2") == 0 || outWord.compare("CHARFREQ") == 0){ 
     cout << "Enter input file name: "; 
     getline(cin, inputFileName); 
     inFile.open(inputFileName.c_str()); 
     while (inFile.get(inChar)) { 
      if (charMap.find(inChar) == charMap.end()) { 
       charMap[inChar] = 1 ; 
      } 
      else { 
       ++charMap[inChar]; 
      } 
     } 
     cout << "Character Frequencies For \"" << inputFileName << "\"" 
      << endl; 
     for (mapIt = charMap.begin(); mapIt != charMap.end(); mapIt++) { 
      total += (*mapIt).second; 
     } 
     cout << "Total bytes read: " << total << endl; 
     for (mapIt = charMap.begin(); mapIt != charMap.end(); mapIt++) { 
      cout << " ('" << (*mapIt).first << "') occurs " 
      << (*mapIt).second << " times (" 
      << static_cast<double> ((*mapIt).second) 
      /static_cast<double> (total) << "% of all characters)" << endl; 
     } 
     inFile.clear(); 
     inFile.close(); 
    } 
    else if (outWord.compare("5") == 0|| outWord.compare("DECRYPT") == 0) { 
     outPutOpened = false; 
     do {     
      cout << "Provide the input file name: "; 

      getline(cin, inputFileName); 

      inFile.open(inputFileName.c_str()); 
      if (inFile.fail()) { 
       cout << "Cannot open file, please try again!" << endl; 
       inFile.clear(); 
      } 
     } 
     while (!inFile.is_open());   
     while (inFile.get(inChar)) { 
      if (inChar < 'a' || inChar > 'z') { 
       inFile.ignore(); 
      } 
      else { 
       inChar -= 'a'; 
       alphabet[static_cast<int> (inChar)]++; 
      } 
     } 
     for (idx = 0; idx < alphabet.size(); idx++) { 
      if(max < alphabet[idx]){ 
       max = alphabet[idx]; 
      } 
     } 
     shiftIt = find(alphabet.begin(), alphabet.end(), max); 

     shiftValue = (distance(alphabet.begin(), shiftIt) - 4); 
     if (shiftValue < 0) { 
      shiftValue += 26; 
     } 
     inFile.close(); 
     do {     
      inFile.open(inputFileName.c_str()); 
      if (inFile.fail()) { 
       cout << "Cannot open file, please try again!" << endl; 
       inFile.clear(); 
      } 
     } 
     while (!inFile.is_open()); 

     outPutOpened = false; 
     do { 
      cout << "Provide the output file name: "; 
      cin >> outputFileName; 
      getline(cin, trash);     
      testStream.clear(); 
      testStream.open(outputFileName.c_str()); 
      if(testStream.good()) { 
       cout << "That file already exists, choose another" << endl; 
       testStream.clear(); 
       testStream.close(); 
      } 
      else { 
       testStream.clear(); 
       testStream.close(); 
       outFile.open(outputFileName.c_str()); 
       if (!outFile.good()) { 
        cout << "bad output"<< endl; 
        outFile.clear(); 
       } 
       if (outFile.good()) { 
        outPutOpened = true; 
       } 
      } 
     } 
     while(!outPutOpened); 

     while((inFile.get(inChar))) { 

      if (inChar >= 'a' && inChar <= 'z') { 


       inChar -= shiftValue; 
       if (inChar < 'a') { 
        inChar += 26; 
       } 

      outFile << inChar; 
      } 
      else if (inChar >= 'A' && inChar <= 'Z'){ 
       inChar -= shiftValue; 
       if (inChar < 'A') { 
        inChar += 26; 
       } 
       outFile << inChar; 

      } 
      else { 
       outFile << inChar; 
      } 

     } 
    } 
    else if (outWord.compare("3") == 0 || outWord.compare("QUIT") == 0) { 
     break; 
    } 
    else { 
     cout << inWord << " is an unrecognized option, please try again" 
     << endl; 
    } 
} 
while (outWord.compare("3") || outWord.compare("QUIT")); 
return 0; 

}

+0

任何人都可以幫我嗎? – 2012-02-07 08:00:42

+1

outFile流派?它在什麼地方關閉了?當你試圖在那裏寫作時,它是否是好的()? – dbrank0 2012-02-07 08:20:14

+0

是outFile流派和不,我只發佈了相關的代碼。唯一一次關閉或打開我已發佈。我已經測試了fail()和good(),並且沒有錯誤出現 – 2012-02-07 20:15:17

回答

4

你必須以刷新流的實際寫入的字符到文件。 你寫到不過outFile-while循環後做:

outFile.flush(); 

..和文本將被寫入罰款的文件。

+0

謝謝!這解決了它。我在其他地方嘗試過沖洗,但我不確定放在哪裏。 – 2012-02-08 10:05:19

+1

(對於谷歌同行),還有'std :: unitbuf'自動刷新功能 – Offirmo 2013-06-06 08:11:14

0

嘗試outFile.close();。它爲我工作。

相關問題