I am wanting to make a book database to record what books I have read. So far, I have a structure for a book entry.
struct Entry
{
string title;
string author;
int pages;
};
As you can see, the title and the author variables are of undetermined size. I would like to store several structures within one file, and then to read all those structures when I want to display the database.
How would I read/write several of these structures from a file? Would I have to have predetermined sizes? Please provide an example.
you could easily store it in a CSV-format the following way:
title_1,author_1,pages_1,title_2,author_2,pages_2,title_3,auth...
when reading back the file, then parse the file according to your separators and you have back your data.
edit : as @Mark Setchell suggested you shouldn't use ' ,
' as a separator, because a comma might also be part of the title itself. instead you should use a more rare character as separator, which isn't used very often in possible book titles. some examples are ' ;
' ' |
' or ' #
' or even unprintable characters
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.