2013-05-02 107 views
0

我正在從文本文件中刪除一行,除了要刪除的用戶指定的一行外,每一行都複製到一個新的文本文件(temp.txt)中,然後刪除原始文本文件( staffMembers.txt)並將temp.txt重命名爲staffMembers.txt。C++如何從文本文件的末尾刪除新行?

staffMembers.txt的內容如下:

1 | First Person | Manager | 123.45 
2 | Second Person | Full Time | 123.45 
3 | Third Person | Casual | 123.45 

當staffMembers.txt的內容複製到TEMP.TXT其計算方法如下:

1 | First Person | Manager | 123.45 
2 | Second Person | Full Time | 123.45 
3 | Third Person | Casual | 123.45 
*** new line *** 

可能有人請解釋一下我如何防止在文本文件末尾創建新行?我的代碼如下:

void deleteStaffMember() 
{ 
    outStream.open("temp.txt", ios::app); 

    if(outStream.fail()) 
    { 
     cout << "Unable to open temp.txt.\n"; 
     exit(1); 
    } 

    inStream.open("staffMembers.txt"); 

    if(inStream.fail()) 
    { 
     cout << "Unable to open staffMembers.txt.\n"; 
     exit(1); 
    } 
    else 
    { 
     cout << endl << "Please enter a staff members ID to delete: "; 
     cin >> deleteLine; 

     while(getline(inStream, line)) 
     { 
      string firstCharacter; 

      firstCharacter += line[0]; 

      if (firstCharacter != deleteLine) 
      { 
       outStream << line << endl; 
      } 
      else 
      { 
       inStream.close(); 

       outStream.close(); 

       cout << "Error. Unable to find staff member.\n" << endl; 

       break; 
      } 
     } 
    } 

    inStream.close(); 

    outStream.close(); 

    remove("staffMembers.txt"); 

    rename("temp.txt", "staffMembers.txt"); 

    cout << "The staff member has been deleted successfully.\n" << endl; 

    system("pause"); 

    system("cls"); 
} 

我不能刪除ENDLoutStream < <線< < ENDL;因爲那時TEMP.TXT的格式如下所示:

1 | First Person | Manager | 123.452 | Second Person | Full Time | 123.453 | Third Person | Casual | 123.45 

回答

1

只要改變邏輯了一下,像這樣:

bool firstLine = true; 

    while(getline(inStream, line)) 
    { 
     string firstCharacter; 

     firstCharacter += line[0]; 

     if (firstCharacter != deleteLine) 
     { 
      if (!firstLine) 
      { 
       outStream << endl; 
      } 
      outStream << line; 
      firstLine = false; 
     } 
     else 
     { 
      inStream.close(); 

      outStream.close(); 

      cout << "Error. Unable to find staff member.\n" << endl; 

      break; 
     } 
    } 

這樣我們打印新行之前的每一行,除了上第一行。

if (firstCharacter != deleteLine) 
{ 
    outStream << line << endl; 
} 

很可能是罪魁禍首:通過使用矢量

string line; 
vector<string> lines; 

// put all lines into a vector 
while (getline(inStream, line)) { 
    lines.push_back(line); 
} 

// remove the line begin with character deleLine.... 
std::remove_if(lines.begin(), lines.end(), 
       [](string &line) { return line[0] == deleteLine; }); 

// append newline to every line except last line 
std::for_each(lines.begin(), line.end() - 1, 
       [](string &line) { line += '\n'; }); 

// write lines to new file 
std::for_each(lines.begin(), line.end(), 
       [](string &line) { outStream << line; }); 
2

清潔的解決方案。

它正在輸出行,然後添加'\ n',這是'endl'所做的事情之一。

考慮「行」是空字符串或幾乎是這樣的情況。

如果您使用此代碼,而不是行爲將變得相當明顯:

if (firstCharacter != deleteLine) 
{ 
    outStream << "{" << line << "}" << endl; 
} 

最後,決定要如何應付這最後一道防線。 yngum的回答非常好,我建議理解他的代碼。 (提示:'\ n'實際上並不意味着「行尾」,它可以解釋爲「行分隔符」,「新行開頭」或「行尾」中的任何一個)

+0

第二個'for_each'可以是'copy(lines.begin(),lines.end(),ostream_iterator (outStream));'這對我來說似乎稍微更具描述性。或者你可以省略第一個'for_each',而不是'copy(lines.begin(),lines.end() - 1,ostream_iterator (outStream,「\ n」)); outStream << lines.back();'只是一個偏好問題。 – 2013-05-02 04:41:38

+0

@JonPurdy你好,趕上。 – yngccc 2013-05-02 04:49:27

相關問題