删除文件中的空行

Delete empty line in file

本文关键字:文件 删除      更新时间:2023-10-16

我有一个文件,我可以添加和删除信息,但是在删除函数(这是一个 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;
}

即使文件中什么都没有,您似乎也在向文件写出一些东西。 与其尝试删除空行,为什么不首先阻止将它们写出来呢? 您可以通过使用计数器来执行此操作,该计数器为未删除的朋友写出多少行,然后只写出这些行。 现在你正在写出整个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 循环中写出文件,甚至没有第二个 for 循环。 那么你甚至不需要数组。它看起来像这样:

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;
}