Ignoring characters within a string

I have a string in this format:"sonic",2019,"PG-13","Action",113,0
within a textfile,

i've been able to use a stringstream to parse each individual value to it's own attribute e.g int and strings.

where there is a comma within the name of the text file.
My code uses a ',' delimiter and once it gets to string such as that, it brings up an error. I also can't edit the text file.
I just want to ignore the , within the movieName string.
I thought of an IF statement but unsure on how to go about it

My code so far is this:
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
vector<string> movieArray;
        string filename = "films.txt";
        string line;
        string tempName;
        string tempYear;
        string tempAgeRating;
        string tempGenre;
        string tempRuntime;
        string tempRating;
        Movie movie;

        ifstream movieList(filename);
        if (movieList.is_open()) {

            cout << "reading file: " << filename << "\n";
            if (!movieList) {
                cout << "File not found, check filePath" << "\n"; //error checking
            }
            while (getline(movieList, line)) {
                if(movieList.peek() == ',')
                stringstream ss(line);
                getline(ss, tempName, ',');
                movie.movieName = tempName;
                getline(ss, tempYear, ',');
                movie.yearReleased = stoi(tempYear);
                getline(ss, tempAgeRating, ',');
                movie.ageRating = tempAgeRating;
                getline(ss, tempGenre, ',');
                movie.genre = tempGenre;
                getline(ss, tempRuntime, ',');
                movie.runTime = stoi(tempRuntime);
                getline(ss, tempRating);
                movie.avgRating = stoi(tempRating);

                ss << movie.movieName
                   << movie.yearReleased
                   << movie.ageRating
                   << movie.genre
                   << movie.runTime
                   << movie.avgRating;

                cout << movie.yearReleased << endl; //print just the years


            }
        }


    }
Last edited on
Use code tags when posting code: http://www.cplusplus.com/forum/articles/16853/

Use the double-quote as a delim instead.
You probably don't really want the double-quotes in your strings anyway.

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
#include <iostream>
#include <sstream>
#include <string>

// Test data
std::istringstream in(
R"("Memento",2000,"R","Mystery/Thriller",113,0
"The Good, the Bad and the Ugly",1966,"APPROVED","Western",161,0
)");

int main()
{
    std::string title, rating, genre;
    int year, runTime, avgScore;
    char ch;

    while (in >> ch)  // read initial double-quote
    {
        getline(in, title, '"');
        in >> ch;  // comma
        in >> year;
        in >> ch >> ch;  // comma and dquote
        getline(in, rating, '"');
        in >> ch >> ch;  // comma and dquote
        getline(in, genre, '"');
        in >> ch;  // comma
        in >> runTime;
        in >> ch;  // comma
        in >> avgScore;

        std::cout << title    << '\n'
                  << rating   << '\n'
                  << genre    << '\n'
                  << year     << '\n'
                  << runTime  << '\n'
                  << avgScore << "\n\n";
    }
}

This is of course assuming that those strings will always be in double quotes and that they will never have a double quote inside the double quotes (even if "escaped").
Last edited on
The thing is, I'm trying to make sure it prints out in the exact same format as text file so I also need the double quotes. My code gives the following output then stops:
1989
1940
1984
1942
which is in line with the text file.
but if i use
1
2
3
4
5
6
7
8
ostringstream oss;
                oss << movie.movieName << ","
                   << movie.yearReleased << ","
                   << movie.ageRating << ","
                   << movie.genre << ","
                   << movie.runTime << ","
                   << movie.avgRating;
cout << oss.str() << endl;



Then an error because of a "," within the movie name

Last edited on
The thing is, I'm trying to make sure it prints out in the exact same format as text file

Then simply put the quotes back when you print those strings.

1
2
3
4
5
6
7
        std::cout << '"'
                  << title    << "\","
                  << year     << ",\""
                  << rating   << "\",\""
                  << genre    << "\","
                  << runTime  << ','
                  << avgScore << '\n';

Last edited on
trying to make sure it prints out in the exact same format as text file so I also need the double quotes.

Maybe std::quoted might be of help to manage std::stringstream input/output having quotes.
https://en.cppreference.com/w/cpp/io/manip/quoted

see also: http://www.cplusplus.com/forum/beginner/250135/
Topic archived. No new replies allowed.