-3

I need some help with reading from a file in visual c++. From "movie.dat", I need to read this:

2015,Star Wars,DVD,120
1987,Star wars,DVD,110
2010,Inception,DVD,100

something like that

with comma (,) delimiter and add to field like:

store.add(year,name,media,length)

In java I would do it like this:

public static void readFromFile(String filename,Store store){
        try {
            Scanner ps = new Scanner(new BufferedInputStream(new FileInputStream(filename)));
            while(ps.hasNextLine()){
                String line = ps.nextLine();
                String field[] = line.split(",");
                store.add(Integer.parseInt(field[0]),field[1],field[2],Integer.parseInt([3]));
            }

            ps.close();
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
eestrada
  • 1,575
  • 14
  • 24
cistysok
  • 49
  • 2
  • 6
  • 2
    Do you want us to convert your java code to c++? If so this really is not a good question for StackOverflow. – drescherjm Jan 07 '16 at 23:22
  • not really i am just more familiar with java, so i tried to code this in java for better understanding.. but i cant find out how to do it in C++ – cistysok Jan 07 '16 at 23:33
  • You want `std::ifstream` and `std::getline`. Start here: [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line). To split on the comma, look into the `std::getline` overload that allows you to specify the delimiter. – user4581301 Jan 07 '16 at 23:43
  • 1
    If you google "stackoverflow read cvs file" you'll find scores of ways to do this.... – user433534 Jan 07 '16 at 23:50

4 Answers4

1

Well, I'd start by defining a struct (or class) to hold the data:

struct movie { 
    int year;
    std::string name;
    std::string media;
    int length;    
};

Then I'd define an extractor to read a single one of those from a file:

std::istream &operator>>(std::istream &is, movie &m) {
    std::string line;
    std::getline(is, line);
    std::istringstream buffer(line);

    char ignore;
    buffer >> m.year >> ignore;
    std::getline(buffer, m.name, ',');
    std::getline(buffer, m.media, ',');
    buffer >> m.length;
    return is;
}

Then I'd read a vector (for example) of those from a file:

std::vector<movie> movies {
    std::istream_iterator<movie>(infile),
    std::istream_iterator<movie>() };
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

You should write your movie class with basic get set function and store the info in vector of movie class object

void readMovieDataFromFile(vector<movie> &movies) {

fstream  inputfile;
std::string line;
inputfile.open("C:\\movie.dat", ios::out | ios::in );   

std::string token;

while (std::getline(inputfile, line)) {
    std::istringstream iss(line);
    while (std::getline(iss, token, ','))
    {
        movies[i].setYear(atoi(token.c_str()));

        std::getline(iss, token, ',');
        movies[i].setName(token);

        std::getline(iss, token, ',');
        movies[i].setMedia(token);

        std::getline(iss, token, ',');
        movies[i].setLength(atoi(token.c_str()));
    }
    i++;
}
inputfile.close();
}
  • Unfortunately, this is not very good advice at all. See the [classic paper on quasi-classes](http://www.idinews.com/quasiClass.pdf) for more about why (and what you should do instead). What you've given above is nearly a perfect example of a quasi-class (and although you may not realize it yet, shows exactly the problems they create). – Jerry Coffin Jan 10 '16 at 01:25
0
void readMovieDataFromFile() {

fstream  inputfile;
std::string line;
inputfile.open("filmy.dat", ios::in);



while (std::getline(inputfile, line)) {
    std::istringstream iss(line);
    std::string token;
    while (std::getline(iss, token, ','))
    {

        int a = atoi(token.c_str());

        std::getline(iss, token, ',');
        char *y = new char[token.length() + 1];
        std::strcpy(y, token.c_str());


        std::getline(iss, token, ',');
        char *c = new char[token.length() + 1];
        std::strcpy(c, token.c_str());


        std::getline(iss, token, ',');
        int b = atoi(token.c_str());
        store.add(a, y, c, b);

    }

}

Thank you all this works for me! :)

cistysok
  • 49
  • 2
  • 6
0

Use a serialization library like boost::serialization. This is really effective for writing and reading objects from a file. A downside would be that it is not as readable.