2014-12-08 54 views
0

我有一個文件,我可以添加和刪除信息,但在刪除功能(這是一個數組50)的每一行沒有任何信息(高達50馬克)被設置爲空行。基本上,如果文件中有3個項目,那麼以前沒有任何內容會添加47行。所以我正在尋找絕對最簡單的方法來通過文件並刪除任何空行。刪除文件中的空行

最簡單的方法可能會很好,因爲我仍在學習C++,並且還不瞭解許多高級功能。

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string friendArray[50]; 
    int counter = 0; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 
    fin.open("moneyFriendsOweMe.txt"); 
    if (fin.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      if (name != currentFriend) 
      { 
       friendArray[counter] = name; 
       counter ++; 
       getline(fin, debtReason); 
       friendArray[counter] = debtReason; 
       counter ++; 
       getline(fin, amountOwed); 
       friendArray[counter] = amountOwed; 
       counter ++; 
      } 
      else 
      { 
       getline(fin, debtReason); 
       getline(fin, amountOwed); 
       successFail = true; 
      } 
     } 

     fin.close(); 
    } 

    fout.open("moneyFriendsOweMe.txt"); 
    if (fout.is_open()) 
    { 
     for(int i = 0; i < 50; i++) 
     { 
      fout << friendArray[i]; 
      fout << "\n"; 
     } 

     fout.close(); 
    } 
    return successFail; 
} 
+0

你是如何使你當前的文件修改?它可能會被修改爲不留空行,並避免需要進行後期處理。 – 2014-12-08 00:32:43

+0

[閱讀文件](http://stackoverflow.com/questions/7868936/read-file-line-by-line)一行一行地閱讀只有一行'\ n'時跳過處理。 – Jens 2014-12-08 00:33:11

+1

你已經試過了什麼 - 發佈了一些代碼,然後讓它更容易指出你的錯誤可能在哪裏 – Adrian 2014-12-08 00:36:14

回答

1

看來你正在向文件寫出一些東西,即使這裏沒有任何東西。而不是試圖去除空行,爲什麼不把它們寫在第一位呢?你可以通過使用計算器來計算你爲沒有被刪除的朋友寫了多少行,然後只寫出這些行。現在,即使你讀了不到50個朋友,你也正在寫出整個50的數組。這會導致額外的行。下面是爲只寫了你所需要的線路代碼:

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string friendArray[50]; 
    int counter = 0; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 
    fin.open("moneyFriendsOweMe.txt"); 
    if (fin.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      if (name != currentFriend) 
      { 
       friendArray[counter++] = name; 

       getline(fin, debtReason); 
       friendArray[counter++] = debtReason; 

       getline(fin, amountOwed); 
       friendArray[counter++] = amountOwed; 
      } 
      else 
      { 
       getline(fin, debtReason); 
       getline(fin, amountOwed); 
       successFail = true; 
      } 
     } 

     fin.close(); 
    } 

    // open and also clear the file so that only the lines you want get written to it 
    fout.open("moneyFriendsOweMe.txt", ios::out | ios::trunc); 

    if (fout.is_open()) 
    { 
     for(int i = 0; i < counter; i++) // only write out the friends you want to keep in the file 
     { 
      fout << friendArray[i]; 
      fout << "\n"; 
     } 

     fout.close(); 
    } 
    return successFail; 
} 

你也可以只寫出來,希望在第一while循環,而不是文件,甚至有環的第二。那麼你甚至不需要數組。它看起來像這樣:

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 

    fin.open("moneyFriendsOweMe.txt"); 

    // open and also clear the file so that only the lines you want get written to it 
    fout.open("moneyFriendsOweMe2.txt", ios::out | ios::trunc); 

    if (fin.is_open() && fout.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      getline(fin, debtReason); 
      getline(fin, amountOwed); 

      if (name != currentFriend) 
      { 
       fout << name << endl << debtReason << endl << amountOwed << endl; 
      } 
      else 
      { 
       successFail = true; 
      } 
     } 

     fin.close(); 
     fout.close(); 
    } 

    return successFail; 
} 
+0

太棒了!這解決了問題。非常感謝你! – 2014-12-08 17:47:22