Search code examples
c++pointersvectorifstream

A Vector of Pointers and ifstream


I am trying to read in data from a text file and store it as a class object I created called Movie. I also have to create a vector of pointers that point to each Movie. I used a while loop that reads until end of file and takes in the data as a Movie. Then I used a pointer to dynamically create a new Movie object which holds the data. It begins to start working and the vector stores a pointer that points to a Movie. After the 19th Movie though it just stops (there are about 300 movies I have to read in). I'm not sure why but I followed it in the debugger. There are no error codes. It just stops and the program does not continue to the end.

//Variables
string title;
//Open input stream
ifstream fin( "CS172_Spring2014_Movie_inputs.txt" ) ;

vector<Movie*> moviePtrList(350);

vector<Movie*>::iterator pos;
pos = moviePtrList.begin();

while(!fin.eof()) 
{
    getline(fin, title, '\n');
    Movie fileMovie(title);
    *pos = new Movie(movieFromFile(fin, fileMovie));

    pos++;
}

I also tried this method:

for(string title; getline(fin,title);) 
{
    /*getline(fin, title, '\n');*/
    Movie fileMovie(title);
    *pos = new Movie(movieFromFile(fin, fileMovie));

    pos++;
}

And this:

for(int i = 0;i<300;i++)
{
    getline(fin, title, '\n');
    Movie fileMovie(title);
    moviePtrList[i] = new Movie(movieFromFile(fin, fileMovie));
    cout << i << endl;
}

I also tried fin.good() instead of fin.eof(). No luck. This is the subprogram I used to read in from the text file:

Movie movieFromFile(ifstream &fin, Movie &inputMovie) 
{

Movie_Rating rating = G;
string actor, director, path, inputRating, separator="$$$$";
unsigned int year, actors;

getline(fin, director, '\n');
inputMovie.setDirector(director);

fin >> year;
fin.ignore();
inputMovie.setYear(year);

getline(fin, inputRating, '\n');
if (inputRating == "G") {
    rating = G ;
} else if (inputRating == "PG") {
    rating = PG ;
} else if (inputRating == "PG13") {
    rating = PG13 ;
} else if (inputRating == "R") {
    rating = R ;
} else if (inputRating == "NC17") {
    rating = NC17 ;
} else if (inputRating == "NR") {
    rating = NR ;
}
inputMovie.setRating(rating) ;
fin.ignore();

getline(fin, path, '\n');
inputMovie.setURL(path);

do {
    getline(fin, actor, '\n');
    inputMovie.addActor(actor);
} while(actor != separator);

return inputMovie;


}

I hope this is not too much text. I probably made a stupid mistake somewhere or overlooked something. I'm going to sleep on it and attack it again in the morning, rewrite the subprogram, and read some more material. Thank you for the help.


Solution

  • Each of the attempts appears to suffer from the same core problem: a misunderstanding about the difference between pointers and objects.

    The proper way to use a vector is to push an object onto it. Often, it's necessary to create that object first. What is mostly not necessary is to use pointers or iterators. So your code might look like:

    vector<Movie> movieList();
    ...
    getline(fin, title, '\n');
    Movie fileMovie(title);
    moveList.push(fileMovie);
    

    Think in terms of objects, not pointers. What you are doing is undefined behaviour and it's not surprising it fails mysteriously after a while. C++ does that.

    There are other problems with the code (see comments) but I think this is the reason it dies. Fix this and maybe ask another question.