readFile() This function should accept the filestream opened in main. The filestream should then be used to loop through the file, inserting each book's information into a book that is then inserted into an array of books. You will likely want to use getline() and string type casting (stoi, stof) here, as cin will stop reading at whitespace. Return the number of lines read from the file. This function should *not* open the filestream.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In C++, how is the readfile function written for this program?

### Function: readFile()

#### Description:
The `readFile()` function is designed to accept the filestream opened in the main function. This filestream will enable looping through the file, extracting each book's information, and inserting this data into a book object, which is then appended to an array of book objects. 

#### Instructions:
1. **Accept the Filestream:** Ensure that the function accepts the filestream that has been opened in the main function.
2. **Process the File:** Use the filestream to read through the file line by line.
3. **Extract Book Information:** Insert the information from each line into a book object.
4. **Store in Array:** Add each book object to an array of books.
5. **Reading Strategies:** Utilize `getline()` and type casting functions like `stoi` and `stof` to handle numerical conversions where necessary, since `cin` stops reading at whitespace.
6. **Return Value:** The function should return the total number of lines read from the file.
7. **Note:** The function should not open the filestream itself; it only works with the filestream passed to it.

#### Example Usage:
```cpp
void readFile(std::ifstream &fileStream, std::vector<Book> &books) {
    // Implementation goes here
}

int main() {
    std::ifstream file("books.txt");
    std::vector<Book> booksArray;
    int linesRead = readFile(file, booksArray);
    // Further processing
}
```

By following these guidelines, the `readFile()` function will efficiently read and process book data from a file, facilitating better data management in your programs.
Transcribed Image Text:### Function: readFile() #### Description: The `readFile()` function is designed to accept the filestream opened in the main function. This filestream will enable looping through the file, extracting each book's information, and inserting this data into a book object, which is then appended to an array of book objects. #### Instructions: 1. **Accept the Filestream:** Ensure that the function accepts the filestream that has been opened in the main function. 2. **Process the File:** Use the filestream to read through the file line by line. 3. **Extract Book Information:** Insert the information from each line into a book object. 4. **Store in Array:** Add each book object to an array of books. 5. **Reading Strategies:** Utilize `getline()` and type casting functions like `stoi` and `stof` to handle numerical conversions where necessary, since `cin` stops reading at whitespace. 6. **Return Value:** The function should return the total number of lines read from the file. 7. **Note:** The function should not open the filestream itself; it only works with the filestream passed to it. #### Example Usage: ```cpp void readFile(std::ifstream &fileStream, std::vector<Book> &books) { // Implementation goes here } int main() { std::ifstream file("books.txt"); std::vector<Book> booksArray; int linesRead = readFile(file, booksArray); // Further processing } ``` By following these guidelines, the `readFile()` function will efficiently read and process book data from a file, facilitating better data management in your programs.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Graphical User Interface
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education