I have a txt file that has information about Movies that I need to save to a database. The text file has things like a string for the title of a movie, an int for the year it was made e.t.c all of the movies are on new lines. My problem is writing the information from that file to my variables. I have get methods for each variable, and a constructor. I also have overloaded the ostream and istream to accept these variables but I do not know how to write the information from the txt file to my variables (I can open the file to the console using ifstream file("filename.txt") but that is as far as my knowledge goes). I also do not know how to make the program read each line and therfore each piece of information seperatly.
I'll post the class I am working in below and what I have so far, as well as the header file. If someone does answer this and knows that code needs to be added to the main (Apart from the usual creating an object process) it would be great if they could add that too! Thankyou:)
(This is for a university assignment that has been very vague in telling us how to do things. I know it might sound like i'm asking for the answers but this is just a small part of the assignment, if I could just get a hint at what to do as we have been given hardly any rescources to look at by our teachers that would be excellent as i'm not sure where else to turn to.)
istream& Movie:: operator>> (istream &input, Movie &m)
Though declared inside the class friends are not part of the class, so you need to remove Movie::
I would implement the reading of the movie like this:
1 2 3 4 5 6 7 8 9
istream& operator >> (istream &input, Movie &m)
{
input >> m.title; // accesible because of the friendship
input >> m.genre;
input >> m.year;
input >> m.rating;
return input;
}
In main()
1 2 3 4 5 6 7 8 9 10 11
ifstream src("YourFilename");
if (!src)
{
perror("open file error");
return -1;
}
Movie movie;
while (src >> movie)
{
// use movie, i.e. add to a vector or sth. else
}
Thankyou Thomas, this has helped a little bit! I knew the 'm.title' was the correct way but it wasn't allowing me to do so, it does now that I have removed the scope operator! Looks like I need to have a look over vectors in c++. Thankyou again:')