Help, i want to do this without using "vector" biblioth

#include<vector>// i dont want to use this !!!
class FileManager{
public:
vector<Utilizator> readUsers(char* filename){
ifstream f(filename);
vector<Utilizator> u;
Utilizator user;
while (!f.eof()){
f >> user;
u.push_back(user);
}
f.close();
return u;
}

vector<Raspuns> readAnswers(char* filename){
ifstream f(filename);
vector<Raspuns> u;
Raspuns r;
while (!f.eof()){
f >> r;
u.push_back(r);
}
f.close();
return u;
}

void saveState(vector<Utilizator> u, vector<Raspuns> r){
ofstream f("utilizatori.dat");
ofstream g("raspunsuri.dat");
for (int i = 0; i<u.size(); i++){
cout << u[i] << endl;
f << u[i] << endl;
}
for (int i = 0; i<r.size(); i++){
cout << r[i] << endl;
g << r[i] << endl;
}
f.close();
g.close();
}

void userReport(vector<Utilizator> u){
ofstream f("raportUtilizatori.txt");
cout << "Nr. utilizatori: " << u.size();
f << "Nr. utilizatori: " << u.size();
f.close();
}

void answerReport(Raspuns r){
ofstream f("raportRaspunsuri.txt");
cout << "Nr. raspunsuri: " << r.size();
f << "Nr. raspunsuri: " << r.size();
f.close();
}

void questionRating(Intrebare& l){
ofstream f("ratingIntrebare.txt");
float rating = 0;
for (int i = 0; i < l.getNrRaspunsuri(); i++)
rating += !(l.getListaRaspunsuri()[i]);
cout << "Rating intrebare " << l.getId() << ": " << rating;
f << rating;
}

};
Why, precisely, do you not want to use an std::vector for storing an arbitrary number of items in a sequence? The alternative (EDIT: that doesn't use any of the other STL containers) is quite tedious.

-Albatross
Last edited on
#include<vector> // i dont want to use this !!!

What do you want to use instead? std::set? std::deque? It's basically the same way as with a vector.

Or perhaps you're asking how to accumulate statistics without reading entire data sets into memory? It would help to be more clear.

By the way, this is wrong:
1
2
3
4
while (!f.eof()){
    f >> user;
    u.push_back(user);
}

The read above will fail when it reaches the end of file, and then you'd push an invalid "user" into u without checking.
In general, "while not eof" kind of loop is an error. The correct loop would be:
1
2
3
while(f >> user) {
  u.push_back(user);
}
Last edited on
Topic archived. No new replies allowed.