简体   繁体   中英

Having Difficulty Reading Data From a File in C++

I'm having some trouble with this project for school and could use some help getting started. I'm given a file that contains movie titles, the year they were made, and their genres then I'm supposed to create a linked list with the information. Each line in the file looks something like this:

1::Toy Story (1995)::Animation|Children's|Comedy

If the user decides to search by the movie title and enters Toy Story , it should display both the name and year of the movie: Toy Story (1995) If the user decides to search by genre it should display every movie that contains the genre. Lastly, if the user wants to remove a movie, they enter the movie title and it should be deleted from the linked list along with its other information.

So the problem I'm having so far is reading in the data. When I read in the data I am able to remove the :: and | and store everything in a vector but now each element of the vector is one word, meaning that if I were to print out vect[1] for example it would be Toy (since vect[0] would be 1 as I don't know how to remove the leading numbers which is another problem).

So how would I go about keeping the title of the movie together so I can add it to a linked list and vice versa for the genres and years? Also, will I need to create three linked lists, one being the movie titles, one being the years and the other being the genres?

I don't have much code, since my problem is right at the beginning but this is what I have so far. It goes through the file and removes the :: and | and stores it into a vector. I don't know how I should precede from here or even if this would be the correct way to start for this type of problem.

   while(inFile >> value)
   {
       istringstream iss(value);
       string line;

       while(getline(iss, line))
       {
           size_t prev = 0, pos;
           while ((pos = line.find_first_of("|:", prev)) != string::npos)
           {
               if (pos > prev)
               {
                   vect.push_back(line.substr(prev, pos-prev));
               }
               prev = pos + 1;
           }
           if (prev < line.length())
           {
                vect.push_back(line.substr(prev, string::npos));
           }
       }
    }

I'm not one to usually ask for help but I'm completely stuck and have no idea how to continue. Any help would be appreciated! If I need to clarify anything let me know.

Don't treat it as a string. Write a data-structure to contain the information.

struct movie {
   std::string title;
   std::string year;
   std::vector<std::string> genres;
};

Then, as you parse each line, fill in an instance of this structure. Once you finish parsing the line, push the structure to your list. This will enable you to do searching later on. You can also add stream operators to the struct for printing it to the screen in a clean format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM