i/o operator overloading reading a file [help needed]

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.)

//Movie.h

#include <iostream>
#include <fstream>
#include <string>

#ifndef MOVIE_H
#define MOVIE_H

using namespace std;

class Movie {
public:
Movie();

Movie(string Title, int Year, string Rating, string Genre, int Time);

void readFile() ;

string getTitle();
int getYear();
string getRating();
string getGenre();
int getTime();

friend ostream &operator << (ostream &output, Movie &m);
friend istream &operator >> (istream &input, Movie &m);

private:

string title;
int year;
string rating;
string genre;
int time;
};

#endif /* MOVIE_H */











//Movie.cpp

#include "Movie.h"

using namespace std;

Movie::Movie() {
}

Movie::Movie(string Title, int Year, string Rating, string Genre, int Time) {
title = Title;
year = Year;
rating = Rating;
genre = Genre;
time = Time;
}

void Movie::readFile() { //I have created a movie object and called this
//function in my main to simply display the text file

ifstream file("movies.txt");
string line;

while (!file.eof()) {
getline(file, line);
cout << line << endl;
}

file.close();
}

string Movie::getTitle() {
return title;
}

int Movie::getYear() {
return year;
}

string Movie::getRating() {
return rating;
}

string Movie::getGenre() {
return genre;
}

int Movie::getTime() {
return time;
}

ostream& Movie:: operator<< (ostream &output, Movie &m) {
output << "Title: " << m.getTitle() << endl;
output << "Genre: " << m.getGenre() << endl;
output << "Year: " << m.getYear() << endl;
output << "Rating: " << m.getRating() << endl;

return output;
}

istream& Movie:: operator>> (istream &input, Movie &m) {

input >> m.getTitle();
input >> m.getGenre();
input >> m.getYear();
input >> m.getRating();

return input;
}
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:')
Topic archived. No new replies allowed.