删除文件中的空行

时间:2014-12-08 00:29:34

标签: c++ file-io

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

1 个答案:

答案 0 :(得分: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循环中写出文件,甚至没有第二个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;
}