0

I have a file I can add and remove information from, but during the remove function (which is an array of 50) each line that doesn't have any information on it (up to the 50 mark) gets set to an empty line. Basically, if there were 3 items in the file, there would be 47 lines of nothing added where nothing was previously. So I am looking for the absolute simplest way possible to go through the file and delete any empty lines.

Simplest way possible would be nice, as I am still learning C++ and don't understand a lot of the advanced features yet.

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;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • How are you making your current file modifications? It might be able to be modified to not leave empty lines, and avoid the need to do post-processing. – Scott Hunter Dec 08 '14 at 00:32
  • [Read the file](http://stackoverflow.com/questions/7868936/read-file-line-by-line) line by line and skip processing when you read a line with a `\n` only. – Jens Dec 08 '14 at 00:33
  • 1
    What did you try already - post some code and then it makes it easier to point out where your errors may be – Adrian Cornish Dec 08 '14 at 00:36
  • I put some code up in the question. Its the section that is causing the extra lines to write. – Jonathan Schmidt Dec 08 '14 at 00:41
  • Also, another error I am finding is that when I run this function and want it to remove a name that has a first and a last name, Like John Doe, it fails. Why is that? – Jonathan Schmidt Dec 08 '14 at 01:23

1 Answers1

1

It seems you are writing something out to the file even when there is nothing there. Rather than try to then remove the empty lines, why not just prevent from writing them out in the first place? You can do this by using the counter of how many lines you wrote out for the friends that were not deleted and then only write out those lines. Right now you are writing the whole array of 50 out even if you read in less than 50 friends. That causes the extra lines. Here is the code for only writing out the lines you need:

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

You could also just write out to the file in the first while loop and not even have a second for loop. Then you don't even need the array. It would look something like this:

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;
}
iCMS
  • 489
  • 4
  • 9