May 14, 2010 at 7:29pm UTC
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 May 14, 2010 at 8:45pm UTC
May 14, 2010 at 7:29pm UTC
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 May 14, 2010 at 7:33pm UTC
May 14, 2010 at 8:54pm UTC
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 May 14, 2010 at 8:59pm UTC
May 14, 2010 at 8:55pm UTC
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 May 14, 2010 at 9:06pm UTC
May 14, 2010 at 9:01pm UTC
Line 8 should be removed. You already passed in the vector that you want to fill as an argument to the function.
May 14, 2010 at 10:08pm UTC
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;
May 14, 2010 at 10:22pm UTC
I already gave you that code 3 posts ago.
May 14, 2010 at 10:25pm UTC
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 May 14, 2010 at 10:30pm UTC