160k views
3 votes
Write in c++ program that read all data from file and adding

data to the file and also print data to file
the code must be dynamic for all situation and can
copy

User Bizna
by
8.0k points

1 Answer

4 votes

To create a C++ program that reads data from a file, adds data to the file, and prints data to the file, you can follow these steps:

1. Start by including the necessary libraries for file operations:
```cpp
#include
#include
using namespace std;
```

2. Declare the file stream objects for reading and writing:
```cpp
ifstream inputFile;
ofstream outputFile;
```

3. Open the file for reading and writing:
```cpp
inputFile.open("data.txt");
outputFile.open("data.txt", ios::app);
```
Note: The "data.txt" file is used as an example. You can replace it with the actual file name.

4. Read data from the file:
```cpp
string data;
while (getline(inputFile, data)) {
cout << data << endl;
}
```
This loop reads each line from the file and prints it to the console.

5. Add data to the file:
```cpp
string newData;
cout << "Enter data to add: ";
getline(cin, newData);
outputFile << newData << endl;
```
This prompts the user to enter data and adds it to the file using the `ofstream` object.

6. Close the file streams:
```cpp
inputFile.close();
outputFile.close();
```

Here's the complete code:
```cpp
#include
#include
using namespace std;

int main() {
ifstream inputFile;
ofstream outputFile;
inputFile.open("data.txt");
outputFile.open("data.txt", ios::app);

string data;
while (getline(inputFile, data)) {
cout << data << endl;
}

string newData;
cout << "Enter data to add: ";
getline(cin, newData);
outputFile << newData << endl;

inputFile.close();
outputFile.close();

return 0;
}
``

The provided C++ code demonstrates how to read data from a file, add data to the file, and print the data to the file.

The code begins by including the necessary libraries for file operations: `iostream` and `fstream`. The `iostream` library allows us to perform input and output operations, while the `fstream` library provides classes to handle file input and output operations.

Next, the code declares two file stream objects: `inputFile` and `outputFile`. `inputFile` is used for reading data from the file, while `outputFile` is used for adding data to the file.

The code then opens the file for reading and writing using the `open()` function. The file name is specified as "data.txt" in this example, but you can replace it with the actual file name you want to work with. The `ios::app` flag is used to append data to the file instead of overwriting it.

To read data from the file, the code uses a `while` loop and the `getline()` function. The `getline()` function reads a line from the file and assigns it to the `data` string variable. The loop continues until there are no more lines to read, and each line is printed to the console using `cout`.

To add data to the file, the code prompts the user to enter data using `cout`. The entered data is stored in the `newData` string variable using the `getline()` function. Then, the `outputFile` object is used to write the `newData` to the file using the `<<` operator. The `endl` manipulator is used to insert a newline character after writing the data.

Finally, the file streams are closed using the `close()` function to free up system resources.

Overall, this code provides a dynamic solution for reading, adding, and printing data to a file in C++.

User Stephan Ahlf
by
7.4k points

No related questions found