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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
//I've already include necessary libs
using namespace std;
struct Movie
{
string title;
string director;
string genre;
string year;
string duration;
} m;
void readMovie(ifstream &inputFile, vector<Movie> &myMovies);
void printMovie(vector<Movie> &myMovies);
bool compareByTitle(Movie lhs, Movie rhs);
int main()
{
ifstream inputFile;
vector<Movie> myMovies;
readMovie(inputFile, myMovies);
sort(myMovies.begin(), myMovies.end(), compareByTitle);
printMovie(myMovies);
return 0;
}
void readMovie(ifstream &inputFile, vector<Movie> &myMovies)
{
string line;
inputFile.open("Movie_entries.txt");
if (!inputFile)
{
cout << "Unable to open the file!";
}
while (getline(inputFile, line)) // reads a line from the file
{
stringstream lineStream(line); // transforms the line into a stream
cout << line << endl;
// get fields from the string stream; fields are separated by comma
getline(lineStream, m.title, ',');
getline(lineStream, m.director, ',');
getline(lineStream, m.genre, ',');
getline(lineStream, m.year, ',');
getline(lineStream, m.duration, ',');
}
inputFile.close();
}
void printMovie(vector<Movie> &myMovies)
{
cout << "\tTitle: " << m.title << endl;
cout << "\tDirector: " << m.director << endl;
cout << "\tGenre: " << m.genre << endl;
cout << "\tyear: " << m.year << endl;
cout << "\tduration: " << m.duration << endl << endl;
}
bool compareByTitle(Movie lhs, Movie rhs)
{
return lhs.title < rhs.title;
}
|