Quick question about vectors

Hey guys! I'm new to the forums and I have a question. I'm trying to create a program that loads users information via text file and outputs them on the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void UserInformation::LoadUsers(const char *filename)
{
    std::ifstream File;

    File.open(filename);

    if (File.is_open())
    {
        while (File.good())
        {
            users.push_back(File.get());
        }
        File.close();
    }
    else
    {
        std::cout <<"Error opening file." << std::endl;
    }

} 



That's the function to load the information from the file and push it into a vector.

The error I am getting is an invalid conversion. I've tried casting it into a char. I'm stumped on what to do...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct User
{
    std::string name;
    int age;

};

class UserInformation
{
    public:
        UserInformation();
        ~UserInformation();

        void GetUserName();
        void GetUserAge();
        void SetUserName();
        void SetUserAge();

        void LoadUsers(const char *filename);
        std::vector<User> users;
    protected:
    private:
};


I would like to have the vector of type User, but I get the same conversion errors. I've looked online and all I found was links on how to create vectors of ifstreams. I probably could've worded my search querey better, but I thought I would ask some pros for advice.

Keep in mind that I still have to write the parsing part of the code, but I can't get past the vector invalid conversion.
ifstream::get returns the ifstream, not a character itself. You really can't cast an ifstream to a User easily.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void UserInformation::LoadUsers(const char *filename)
{
    std::ifstream File;

    File.open(filename);

    if (File.is_open())
    {
        while (File.good())
        {
            User temp;
            std::getline(File, temp.name);
            users.push_back(temp);
        }
        File.close();
    }
    else
    {
        std::cout <<"Error opening file." << std::endl;
    }

} 
Oh, thanks for the help!
Topic archived. No new replies allowed.