Display all data from text file

Hi guys.

im kinda stuck with a problem here.
Just had a code created (thanks to cplusplus +1)
Im trying to create a sort of membersystem.
So i have all the members with their date (username etc.)
in a .txt file.
How can I display all data from the .txt file as output?
I thought i might need a for-loop to display them all, till none is left.
Am i correct?

Could you please help me?

EDIT: Ok, Ive written some code to show all the data from the file.
It works now, but.
I want the data show in a colomn.
How can i do this?

Current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void display_members()
{
    string getcontent;
    ifstream openfile ("members.txt");
    if(openfile.is_open())
    {
        while(! openfile.eof())
        {
            openfile >> getcontent;
            cout << getcontent << endl;
        }
    }
}
Last edited on
Im trying to create a sort of membersystem.
If you want to sort the content of the file, you need to read it into memory (into some data structure) and sort it there.

How can I display all data from the .txt file as output?
You need to write it to stdout (with std::cout). You should realise that the way you read the file will get you strings that are delimited by space, tab and new line. It won't read the whole line.

I thought i might need a for-loop to display them all, till none is left.
You already have a loop to read the file. If all you wanted to do was write it out, you could use the same loop. But as you want to sort it first, you'll need another loop.
operator>> only reads till the first space, so is no use for reading lines. Try getline() instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
void display_members()
{
    string getcontent;
    ifstream openfile ("members.txt");
    if(openfile.is_open())
    {
        while(! openfile.eof())
        {
            getline(openfile, getcontent);
            cout << getcontent << endl;
        }
    }
}


Or even

1
2
3
4
5
6
7
8
        ...

        while(getline(openfile, getcontent))
        {
                cout << getcontent << endl;
        }

        ...

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void display_members(Member member_array[], int amountofmembers)
{
    assert (amountofmembers > 0);
    string gender_and_paid;
    cout << "Surname \t Forename \t Street+housenr \t Postcode \t City \t M/F \t Paid" <<
        "____________________________________________________________________________";
        for (int counter=0; counter < amountofmembers; counter++)
        {
            if (member_array[counter].gender == Male)
            {
                gender_and_paid = "M";
            }
            else
            {
                gender_and_paid = "F";
            }
            if (member_array[counter].paid)
            {
                gender_and_paid += "yes";
            }
            else
            {
                gender_and_paid += "No";

            }
       cout << member_array[counter].surname << " "<< member_array[counter].forename  << " \t" << member_array[counter].street_name << " \t" << member_array[counter].postcode
            << " \t" << member_array[counter].city_name << "\t"  << member_array[counter].year_of_birth << " \t";

        }
}


I created this.
But it gives an error:
error: too few arguments to function 'void display_members(Member*, int)'
What is wrong?

Thanks for responses guys :D
You need to show how you've called it.
Topic archived. No new replies allowed.