Searching text files for items of same type

Jun 15, 2016 at 1:59am
Hi everyone!

So, I am trying to run a function that would search a file that is filled with data in this format:

// each line is a library item.
type unique-id title | author-or-artist-name
BOOK 43045 Computer Science Hits | Neil Dale and John Lewis
BOOK 47341 How Computers Work | Ron White
REF 48462 Encyclopedia Britannica | Britannica Inc.
REF 47344 National Geographic Computer | Lucy Spelman
BOOK 48450 Java: A Beginner's Guide | Herbert Schildt
CD 48452 Traveller | Chris Staphleton
CD 48453 How Can It Be Java | Lauren Daigle
REF 48460 The Merriam-Webster Dictionary | Merriam
BOOK 45552 Python Programming | John Zelle
CD 45553 I Still Do Programming | Eric Clapton
BOOK 45557 Beginning Software Engineering | Rod Stephens
REF 45559 A dictionary of nutrition | David Bender
CD 45561 Best Eagles Dictionary Songs | Eagles
CD 46555 The Ultimate Computer Hits | Garth Brooks

The function would check for a specific type of item (listed in the first column) and display the information of all items with that type. So my question is:
Is the best method of listing one type of item from a .txt to search the file for that word, then put all those into a vector and display to user? If not, how should I do this?
Last edited on Jun 15, 2016 at 1:59am
Jun 15, 2016 at 4:38am
I think it will be better to list one type of item from .txt to search the file for that word and then display the user.
Jun 15, 2016 at 6:17am
closed account (48T7M4Gy)
Is the best method of listing one type of item from a .txt to search the file for that word

It would work but unlikely to be seen as very practical for a real-life/professional solution.

then put all those into a vector and display to user?

You might need to split the lines into more than two columns but it would be a good way to go, <maps> too might be worth a look because they are accessible using key values so are quicker to access.
Last edited on Jun 15, 2016 at 8:22am
Jun 15, 2016 at 8:20am
I would create a struct Media, read the file line by line into the struct and store it in a vector. Then you can easily use the find, sort algorithms of the stl.
1
2
3
4
5
6
7
struct Media
{
   string type;
   int id;
   string title;
   string author;
}
Topic archived. No new replies allowed.