2012-09-30 101 views
-1

我試圖完成沒有太多經驗CSE實驗室與I/O或使用文本文件中的行。該計劃的目標是根據用戶反饋的詞彙和定義創建詞典。輸出文本文件的格式應該是覆蓋和追加在文本文件(C++)

條目數(如一個數字)

定義

定義

等。

在這一點上,輸出讀取,如果你所有的話是 「阿爾伯塔省」 和定義 「三人行」:

阿爾伯塔

三人

2Alberta

三人

2Alberta

三人

東西顯然是非常錯誤的。下面是結果代碼:如果它很混亂並且輸出語句被分開,我很遺憾,我一直試圖弄清楚這一點。

#include <iostream> 
#include <string> 
#include <fstream> 
#include <stdlib.h> 

using namespace std; 
int main() 
{ 
    string name; // Name of writing/reading document 

cout << "Please enter the path (including file name) for the dictionary you want to create: " ; 
getline(cin,name); // Inputs name 

fstream dicFile; 

int addcount = 0; // number of times file has been added to. 

int choice = 0; // Controls while loop 

while (choice != 3) 
{ 
    cout << "- - - - - - - - - -" << endl; 
    cout << "1) Add a word to the dictionary" << endl; 
    cout << "2) Print Dictionary" << endl; 
    cout << "3) Quit" << endl; 
    cout << "Choose an option from the menu: "; 

    string choiceS; 
    getline(cin,choiceS); 

    choice = atoi(choiceS.c_str()); 

    string wordAdd; // Stores word to be added 
    string defAdd; // Stores definition to be added 

    int size = 1; // Number of entries after the first entry 

    if(choice > 0 && choice < 4) 
    { 
     cout << "- - - - - - - - - -" << endl; 

     if(choice == 1) 
     { 

      if(addcount == 0) 
      { 
       dicFile.open(name.c_str(),ios::in|ios::out|ios::app); 
       cout << "Enter a word or phrase to add to the dictionary: "; 
       getline(cin,wordAdd); 
       dicFile << size; 
       dicFile << endl; 
       dicFile << wordAdd; 

       dicFile << endl; 

       cout << "Enter the definition for \"" << wordAdd << "\": "; 
       getline(cin,defAdd); 
       dicFile << defAdd << endl; 

       dicFile.close();  
       addcount++; 
      } 

      else 
      { 
       dicFile.open(name.c_str(),ios::in|ios::out|ios::app); 
       size = size + 1; 
       dicFile << size; 
       dicFile.close(); 

       dicFile.open(name.c_str(),ios::in|ios::out|ios::app); 
       cout << "Enter a word or phrase to add to the dictionary: "; 
       getline(cin,wordAdd); 
       dicFile << wordAdd; 

       dicFile << endl; 

       cout << "Enter the definition for \"" << wordAdd << "\": "; 
       getline(cin,defAdd); 
       dicFile << defAdd << endl; 

       dicFile.close(); 
      } 


     } 

     if(choice == 2) 
     { 
      if(true) 
      { 
       dicFile.open(name.c_str(),ios::in); 

       string readSize; 
       int readSizei; 

       getline(dicFile,readSize); 
       readSizei = atoi(readSize.c_str()); 

       for(int i = 0; i < readSizei*2 + 1; i++) 
       { 
        string word; 
        string def; 
        getline(dicFile,word); 
        cout << word << endl; 
        getline(dicFile,def); 
        cout << def << endl; 
       } 
       dicFile.close();  
      } 

      else 
      { cout << "- - - - - - - - - -" << endl; 
       cout << "The dictionary is empty!" << endl; 
      } 
     } 

    } 

    else 
    { cout << "- - - - - - - - - -" << endl; 
     cout << "Invalid menu selection (number should be between 1-3)" << endl; 
    } 

} 
} 

我有什麼,以覆蓋文本文件的第一行,但附加到文件的新定義,當我選擇選項2,怎麼辦?相當新的I/O。謝謝你的時間。

回答

1

您無法「現場」修改現有文件。相反,您必須創建一個新的臨時文件,並將所有內容複製到臨時文件中,並進行所需的修改。然後將臨時文件重命名爲原始文件,從而替換它。

在僞代碼:

open_old_file(); 
create_temporary_file(); 

while (read_line_from_old_file()) 
{ 
    do_possible_modifications_to_line(); 
    write_line_to_temporary_file(); 
} 

close_temporary_file(); 
close_old_file(); 

rename_temporary_file_to_old_file(); 
// or if the above doesn't work: 
// copy_temporary_file_to_old_file(); 
// delete_temporary_file(); 
+0

這聽起來很簡單,但我該怎麼做呢? – Kotsuzui

+0

非常感謝您的時間,我會盡力。 – Kotsuzui