0

I'm reading in Netflix shows from a csv file into my object array using getline. Some of the strings have two commas so when getline encounters the first one it stops halfway through the string splitting it. For example, "crude and sexual humor, language and some drug content" would be separated into "crude and sexual humor" and "language and some drug content". This affects the rest of the data as getline starts again at the wrong point in the file. Is there a way I can ignore the first comma so the full string is stored in the proper variable within the array?

These are the first three rows in the csv file:

White Chicks,PG-13,crude and sexual humor, language and some drug content,80,2004,82,80

Lucky Number Slevin,R,strong violence, sexual content and adult language,100,2006,NA,82

Grey's Anatomy,TV-14,Parents strongly cautioned. May be unsuitable for children ages 14 and under.,90,2016,98,80

This is the code in main.cpp

#include <iostream>
#include "Video.h"
using namespace std;

int main() {
    Video playlist;
    Video Netflixplaylist[1000];//Declare Video array
    playlist.CreatePlaylist(Netflixplaylist);
    cout << Netflixplaylist[0].media1.title << endl;
    cout << Netflixplaylist[1].media1.title << endl;//Test if data was properly stored
    return 0;
}

This is the code in Video.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

#include "Video.h"//Inlcude Video.h file
void Video::CreatePlaylist(Video playlist[1000]){
    ifstream myfile;
    int row = 0;
    myfile.open("/Users/Auditore/Desktop/Netflix_Shows.csv");
    while(!myfile.eof()){
        for(row; row < 1000; row++){
            getline(myfile, playlist[row].media1.title, ',');
            getline(myfile, playlist[row].media1.rating, ',');
            getline(myfile, playlist[row].media1.rating_lv, ',');
            getline(myfile, playlist[row].media1.description, ',');
            getline(myfile, playlist[row].media1.release_year, ',');
            getline(myfile, playlist[row].media1.user_rate, ',');
            getline(myfile, playlist[row].media1.user_rate_size, '\n');
        }
    }
}

This is the code in Video.h

#ifndef Video_h
#define Video_h
#include <fstream>
using namespace std;
struct MediaInfo{
    string title;
    string rating;
    string description;
    string rating_lv;
    string release_year;
    string user_rate;
    string user_rate_size;
};
class Video {//Video class
private:
    ifstream myfile;
public:
    std::string playlistname = "My Bored Playlist";//Set playlistname to Boring Playlist
    MediaInfo media1;
    void CreatePlaylist(Video playlist[1000]);
};

#endif
Tarracon
  • 35
  • 4
  • Is it always the first field? Perhaps you can preprocess the data to escape the offending comma somehow. For example, surround each field in double quotes use that as the delimiter on the first field. In general, there is no way to distinguish whether the embedded comma is a delimiter or a part of the field – Matthew Fisher Sep 23 '18 at 01:26
  • 2
    You can't if specifying the terminator as `','` - `geline` will only do exactly what you tell it to. You can however, read without the `','` terminator into a `string` and then parse the contents in any way that avoids the problem. Handling a line at a time and storing the line then allows you to employ any number of checks on the line to get what you need out of it. See also, [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) – David C. Rankin Sep 23 '18 at 02:32

0 Answers0