Home > Mobile >  How to read from file in c and insert data from file into vector thats is type of an class?
How to read from file in c and insert data from file into vector thats is type of an class?

Time:10-06

Like the title says I need to insert data from file into vector that is a type of class I created. Here is my code:

#include <iostream>
#include <fstream>
#include "Film.h"
#include "Comedy.h"
#include <string>
#include <vector>
using namespace std;
Comedy k;
Film f;

int main() {
    vector <Comedy> comedy;
   

    ifstream Comedies("Comedy.txt", ios::in);
    int number = 0;
    string line;
    while (getline(Comedies, line)) {
        br  ;
        komedija.push_back(line); // this is where I get the error
    }
   

This is Comedy class:

#pragma once
#include "Film.h"
#include "Actor.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;

enum Type2 {black = 1, romantic, child};

class Comedy :
    public Film
{
private:
    vector<Actor>actors;
    Type2 typeComedy;
public:
    void setActors();
    vector<Actors>& getActors();
    void setType();
    Type2 getType();
    void insert();
};
 

Anyone know what is the issue, I assume it is because the line variable is a type of string, and I am trying to insert into a vector that is not type of string, how do I then insert data from file into a vector that is not string?

This is a Film class:

#pragma once
class Film
{
private:
    char name[25];
    int timeOfDuration;
    char startMovieTime[7];
public:
    Film();
    void setName();
    char* getName();
    void setTimeOfDuration();
    int getTimeOfDuratiion();
    void setStartMovieTime();
    char* getStartMovieTime();
    ~Film() {};
};

CodePudding user response:

Assuming Film has a constructor that takes a std::string, I suggest adding it to the derived classes:

Example with Comedy:

class Comedy : public Film {
public:
    using Film::Film; // add the `Film` constructor(s)
    // ...
};

Then adding a new Comedy can be done using std::vector::emplace_back:

komedija.emplace_back(line);

Demo

If you for some reason want to store the film name in a char[], you can still use a constructor that accepts a std::string and stores it in your char[].

Example:

#include <algorithm> // std::copy_n, std::min

class Film {
private:
    char name[25];

public:
    Film() : name{} {} // initialize name

    // added constructor:
    Film(const std::string& Name) : Film() {
        std::copy_n(Name.c_str(), std::min(sizeof name - 1, Name.size()), name);
    }
};

Note: Unless Comedy has some special member variables/functions I suggest adding a category member to Film instead of inheriting from Film.

CodePudding user response:

I find the best way is to use the iterator constructor of vector

int main()
{
    std::ifstream       comedyFile("Comedy.txt")
    std::vector<Comedy> comedy(std::istream_iterator<Comedy>(comedyFile),
                               std::istream_iterator<Comedy>{});

}

This assumes that the Comedy has an operator>> defined that will read from a stream into a default constructed Comedy object.

class Comedy
{
    public:
        Comedy(){}  // creates a default object.
        friend std::istream& operator>>(std::istream& stream, Comedy& obj) {
            obj.load(stream);
            return stream;
        }
    private:
        void load(std::istream& stream) {
            // read from the stream into your object.
        }
};
           

Sometimes creating a default object is not practical or nice. In this case you can create a "Reader" object that reads data and allows construction of the object.

  int main()
{
    std::ifstream       comedyFile("Comedy.txt")
    std::vector<Comedy> comedy(std::istream_iterator<ComedyReader>(comedyFile),
                               std::istream_iterator<ComedyReader>{});

}

This assumes that the ComedyReader has an operator>> defined that will read from a stream. You can then store the data from the file in a way that is useful and then construct the object when it is pushed into the vector (as this will cause a cast you just need to be able to cast the object into a Comedy).

class ComedyReader
{
    std::string  dataForComedy;
    friend std::istream& operator>>(std::istream& stream, ComedyReader& reader) {
        std::getline(stream, dataForComedy);
        return stream;
    }
    // Now we need to be able to create a Comedy from a string.
    operator Comedy() const {
        return Comedy(std::move(dataForComedy));
    }
};

class Comedy
{
    public:
        Comedy(std::string data) {
            // read from the string into your object.
        }
};
      

When moving objects around it is always nice to provide move constructors (though sometimes copy constructors will suffice).

class Comedy
{
    public:
        // Move Operators
        Comedy(Comedy&& src)            noexcept;
        Comedy& operator=(Comedy&& src) noexcept;

        // Copy Operators
        Comedy(Comedy const& src);
        Comedy& operator=(Comedy const& src);
};
  •  Tags:  
  • c
  • Related