简体   繁体   中英

Reading from a File in C++, Dividing the Input

Number of Copies
Movie Title
Movie Actor
Movie Actor
Movie Director
Movie Producer
Movie Production Co.
Number of Copies
Movie Title
Movie Actor
Movie Actor
Movie Director
Movie Producer
Movie Production Co.

Customer1 ID
Customer2 ID 
Customer3 ID 
Customer4 ID

I have a File I/O question for C++. If I have an input file that looks like this, what commands can I use to stop inserting getlines() into variables from the top section in order to begin inserting getlines() into variables from the bottom section? In pseudocode I can imagine it would be something like

while (the next line isn't "\n"), keep adding to the movie database.

then

while (!fin.eof()), start adding to the customer database

But I don't know how to write that first while condition. My book only teaches us how to use get and getline for File I/O... and obviously something like

while (fin.get() != "\n")

doesn't work due to the operand types. Sorry if my question is rather poorly worded, it's just how I've thought of approaching it. Thanks.

You can use std::getline(std::istream&, std::string&) to fetch a line and save it in a std::string . std::string provides a method empty to check whether the string is empty.

std::getline won't extract the delimiter, so it should be easy for you to check whether a line is empty.

(Hint for extractions: In order to extract substrings from your line you can use a std::istringstream .)

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