我有一個文件,我可以添加和刪除信息,但在刪除功能(這是一個數組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;
}
你是如何使你當前的文件修改?它可能會被修改爲不留空行,並避免需要進行後期處理。 – 2014-12-08 00:32:43
[閱讀文件](http://stackoverflow.com/questions/7868936/read-file-line-by-line)一行一行地閱讀只有一行'\ n'時跳過處理。 – Jens 2014-12-08 00:33:11
你已經試過了什麼 - 發佈了一些代碼,然後讓它更容易指出你的錯誤可能在哪裏 – Adrian 2014-12-08 00:36:14