ifstream best way to read without memory usage

2019-07-21 06:30发布

I have a textfile that contains authors and the books written by authors. I am assigned to write a program in that the user will provide the name of a author. And the program must print the name of any books written by that author.

I understand that i am supposed to use an ifstream to read this information. But how can i make it so my program doesn't read the entire file into memory (array, vector, etc.) to perform any of the search queries?

What would be the best way to approach this? my program should use classes as well.

2条回答
太酷不给撩
2楼-- · 2019-07-21 07:02

A lot here depends on how often you're going to look for books from the file. If yo're only going to look for one or two, then the most sensible method is probably to just scan through the file reading pairs of lines to find the ones you want.

Most other methods assume that you're going to look for data in the file often enough to justify spending some extra time up front to optimize the queries later. Assuming that's correct, one possibility would be to create an index by reading through the file, hashing each author's name, and the position in the file of the "record" for that author/book pair.

Then you'll store those pairs of hash/file offset into a separate file. When you want to do a query, you'll read the hashes/files offsets into memory. Hash the name of the author you're searching for (using the same algorithm) and see which (if any) file offsets have the same hash value. Seek to those spots in the file, and read in the record for the book. At that point, re-compare the author name in the file to the author name that was entered, in case of a hash collision. Show the records where you get a match.

查看更多
霸刀☆藐视天下
3楼-- · 2019-07-21 07:20

I don't know the whole answer, or even the syntax, but a good way to get started is what do you know about the format of the input text file? Is it simply a two-column file like: [Author Book] separated by a common delimiter? In that case, you could construct a loop that goes through the whole file and only store the entries into a vector that match the search string.

查看更多
登录 后发表回答