HALP!!1 Loading a Vector <struct> x

My problem seems simple enough

I am saving my vector of structs vector<day> may to a .txt file


I cant get a working load fuction
I understand I need to push_back the data, but just cannot get it working
Note my knowledge of the fstream is very minimal

I need an example of code that could load my structs


here is my relavent code:

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
31
32
#include <iostream>
#include "CinReader.h"
#include <string>
#include <vector>
#include <fstream>

using namespace std;

CinReader reader;

struct day
{
    int date;
    string entry;
};


void save (vector<day> may)
{
ofstream fout("journalSave.txt");

    if (!fout.fail())
    {
        for (int i = 0; i < 31; i++)
        {
            fout << may[i].date << endl;
            fout << may[i].entry << endl;

        }
        fout.close();
    }
}


Please can anyone show me how I can use a for loop to push back this data to my struct
THANKS IN ADVANCE!!!
Last edited on
D'OH!

@Line 1: Missing semicolon.

Also, to use push_back, just create a temporary struct somewhere in your function, read your data to that, and push_back() a copy of that into your vector. The solution you have now will not work because vectors start out with zero elements (although you could push back the vector with empty structs).

Other solutions and problems may exist.

Also, we don't give out full or partial code solutions here. Sorry.

-Albatross
Last edited on
Your for loop on line 25 would be better using the size taken from the vector its accessing like this:
 
for (int i = 0; i < may.size(); i++)


Its much safer.
In order to read one entry into a struct day you might use this:
1
2
3
day d;
ifs >> d.date >> ws;
std::getline(ifs, d.entry);


Then you can push_back() d onto your vector.
Last edited on
Ok I dont need a full or partial code solution.

note the vector is initialized to a size of 31 already

What I would like is an example of how to use the f stream to bring data back into a vector
something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void retrieve (vector<day>& may)
{
ifstream fin("journalSave.txt");
    if (!fin.fail())
    {
        for (int i = 0; i < 31; i++)
        {
             day d;
             d.date = i+1;
             d.entry = ???????????????????????

            may.push_back(d);
        }

        fin.close();
    }
}



also I understand that may.size would make my program more dynamic its not an issue since the size will always be 31
Last edited on
Line 8 should be removed. You already passed in the vector that you want to fill as an argument to the function.
can nobody tell me the exact code to do a fin on a vector of type struct?!


i tried

std::getline(inf, d.entry)


and
fin >> may[i].entry >> endl;

I already gave you that code 3 posts ago.
For d.entry you need std::getline.

But after you read in d.date with in >> d.date you are left with the newline character stuck between you and your next read operation.

So that needs to be absorbed like this:

1
2
3
in >> d.date;
in >> std::ws; // absorb newline character
std::getline(in, d.entry); // now safely read the entry 
Last edited on
first let me say thanks again galik!
but
I have been trying it and it wont work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void retrieve (vector<day>& may)
{
ifstream fin("journalSave.txt");
    if (!fin.fail())
    {
        for (int i = 0; i < 31; i++)
        {
            day d;
            fin >> std::ws;
            std::getline(fin, d.entry);
            std::cout << may[i].entry << std::endl;
            d.date = (i+1);

            may.push_back(d);
        }

        fin.close();
    }
}


also tried it exactly how you sugested with (in, d.entry)
wouldnt compile said that in wasnt declared in this scope
Topic archived. No new replies allowed.