In C++, my program reads from a file, each line of the text file ends in a 1 or 0. How do you make a function that updates the value in the text file to change to from 1 to 0 or 0 to 1?
In C++, my program reads from a file, each line of the text file ends in a 1 or 0. How do you make a function that updates the value in the text file to change to from 1 to 0 or 0 to 1?
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
Related questions
Question
In C++, my
![### Sample C++ Code: Handling File Input
```cpp
#include "helpers.h"
int main (int argc, char* argv[]) {
Anime shows[SIZE];
ifstream fin (argv[1]);
int option, count = 0;
if (fin.fail()) {
cout << "File name is incorrect or does exist. Please try again." << endl;
return 1;
}
readFile(fin, shows, &count);
fin.close();
ofstream fom (argv[1]);
do {
displayStartMenu();
```
### Explanation
1. **Library Inclusion**
```cpp
#include "helpers.h"
```
- This line includes a header file named `helpers.h` which likely contains function prototypes and definitions.
2. **Main Function**
```cpp
int main (int argc, char* argv[]) {
```
- The `main` function is the entry point of the program. It takes command line arguments (`argc` for argument count and `argv` for argument vector).
3. **Variable Declaration**
```cpp
Anime shows[SIZE];
ifstream fin (argv[1]);
int option, count = 0;
```
- An array `shows` of type `Anime` with a size defined by `SIZE` is declared.
- An input file stream `fin` is initialized with the first command line argument (`argv[1]`), which should be the path to the file.
- Two integer variables `option` and `count` are declared and initialized to zero.
4. **File Error Handling**
```cpp
if (fin.fail()) {
cout << "File name is incorrect or does exist. Please try again." << endl;
return 1;
}
```
- The program checks if the file failed to open using the `fail()` method. If it did, an error message is printed, and the program returns `1` to indicate an error.
5. **File Reading**
```cpp
readFile(fin, shows, &count);
```
- The function `readFile` is called to read from the file into the `shows` array. `count` is passed by reference to keep track of the number of items read.
6. **Closing the File**
```cpp
fin.close();
```
- The input](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff462e736-7a38-4abe-8728-e08b1ac68dd9%2F0a33a124-f893-4b67-ae7c-b3b8f021c86a%2Frnjrooi_processed.png&w=3840&q=75)
Transcribed Image Text:### Sample C++ Code: Handling File Input
```cpp
#include "helpers.h"
int main (int argc, char* argv[]) {
Anime shows[SIZE];
ifstream fin (argv[1]);
int option, count = 0;
if (fin.fail()) {
cout << "File name is incorrect or does exist. Please try again." << endl;
return 1;
}
readFile(fin, shows, &count);
fin.close();
ofstream fom (argv[1]);
do {
displayStartMenu();
```
### Explanation
1. **Library Inclusion**
```cpp
#include "helpers.h"
```
- This line includes a header file named `helpers.h` which likely contains function prototypes and definitions.
2. **Main Function**
```cpp
int main (int argc, char* argv[]) {
```
- The `main` function is the entry point of the program. It takes command line arguments (`argc` for argument count and `argv` for argument vector).
3. **Variable Declaration**
```cpp
Anime shows[SIZE];
ifstream fin (argv[1]);
int option, count = 0;
```
- An array `shows` of type `Anime` with a size defined by `SIZE` is declared.
- An input file stream `fin` is initialized with the first command line argument (`argv[1]`), which should be the path to the file.
- Two integer variables `option` and `count` are declared and initialized to zero.
4. **File Error Handling**
```cpp
if (fin.fail()) {
cout << "File name is incorrect or does exist. Please try again." << endl;
return 1;
}
```
- The program checks if the file failed to open using the `fail()` method. If it did, an error message is printed, and the program returns `1` to indicate an error.
5. **File Reading**
```cpp
readFile(fin, shows, &count);
```
- The function `readFile` is called to read from the file into the `shows` array. `count` is passed by reference to keep track of the number of items read.
6. **Closing the File**
```cpp
fin.close();
```
- The input
![### Available Anime Shows and Pricing
1. **Naruto Season 1**
- Price: $2.99
- Availability: Checked out (1)
2. **Naruto Season 2**
- Price: $2.99
- Availability: Checked out (1)
3. **Naruto Season 3**
- Price: $2.99
- Availability: Checked out (1)
4. **Yu Yu Hakusho Complete**
- Price: $15.99
- Availability: Checked out (1)
5. **Astro Boy**
- Price: $2.99
- Availability: Checked out (1)
6. **Inuyasha Complete**
- Price: $15.99
- Availability: Available (0)
7. **Dragon Ball Complete**
- Price: $20.99
- Availability: Checked out (1)
8. **Dragon Ball Z Complete**
- Price: $50.99
- Availability: Available (0)
9. **Kimi Ni Todoke**
- Price: $2.99
- Availability: Checked out (1)
10. **K-On!**
- Price: $2.99
- Availability: Available (0)
### Code Explanation
The image also contains two functions written in C++ that manage the return and update processes of anime shows.
#### 1. `returnItem` Function
The `returnItem` function handles the return of an anime show:
- **Parameters:**
- `ofstream &fon`: Output file stream for writing data to a file.
- `Anime shows[]`: Array of anime shows.
- `int* option`: Pointer to the option selected by the user.
- **Functionality:**
- Check if the anime show is already checked out by verifying the `getCheckedOut()` method.
- If the item is not checked out (denoted by "0"), print an error message.
- If the item is checked out (denoted by "1"), change the status to "0" and update the file using `writeToFile`.
#### 2. `writeToFile` Function
The `writeToFile` function updates the status of an anime show in the file:
- **Parameters:**
- `ofstream &fon`: Output file stream for writing data to a file.
- `Anime](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff462e736-7a38-4abe-8728-e08b1ac68dd9%2F0a33a124-f893-4b67-ae7c-b3b8f021c86a%2Fqkc1ufa_processed.png&w=3840&q=75)
Transcribed Image Text:### Available Anime Shows and Pricing
1. **Naruto Season 1**
- Price: $2.99
- Availability: Checked out (1)
2. **Naruto Season 2**
- Price: $2.99
- Availability: Checked out (1)
3. **Naruto Season 3**
- Price: $2.99
- Availability: Checked out (1)
4. **Yu Yu Hakusho Complete**
- Price: $15.99
- Availability: Checked out (1)
5. **Astro Boy**
- Price: $2.99
- Availability: Checked out (1)
6. **Inuyasha Complete**
- Price: $15.99
- Availability: Available (0)
7. **Dragon Ball Complete**
- Price: $20.99
- Availability: Checked out (1)
8. **Dragon Ball Z Complete**
- Price: $50.99
- Availability: Available (0)
9. **Kimi Ni Todoke**
- Price: $2.99
- Availability: Checked out (1)
10. **K-On!**
- Price: $2.99
- Availability: Available (0)
### Code Explanation
The image also contains two functions written in C++ that manage the return and update processes of anime shows.
#### 1. `returnItem` Function
The `returnItem` function handles the return of an anime show:
- **Parameters:**
- `ofstream &fon`: Output file stream for writing data to a file.
- `Anime shows[]`: Array of anime shows.
- `int* option`: Pointer to the option selected by the user.
- **Functionality:**
- Check if the anime show is already checked out by verifying the `getCheckedOut()` method.
- If the item is not checked out (denoted by "0"), print an error message.
- If the item is checked out (denoted by "1"), change the status to "0" and update the file using `writeToFile`.
#### 2. `writeToFile` Function
The `writeToFile` function updates the status of an anime show in the file:
- **Parameters:**
- `ofstream &fon`: Output file stream for writing data to a file.
- `Anime
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps

Knowledge Booster
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
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education