read write in file in C++

PHOTO EMBED

Tue Jan 03 2023 05:41:44 GMT+0000 (Coordinated Universal Time)

Saved by @berasumit611 #c++

#include <fstream>
#include <iostream>
using namespace std;


int main() {

    // Part 1: Write on a file.
    ofstream oFile;
    oFile.open("my-bio.txt");

    string name;
    int age;

    // User input. 
    cout << "Enter name: ";
    getline(cin, name);

    cout << "Enter age: ";
    cin >> age;

    oFile << "My name is " << name << ". \n";
    oFile << "I am " << age << " years old. \n";

    oFile.close();

    // Part 2: Read from a file.
    string str;

    ifstream iFile;
    iFile.open("my-bio.txt");

    cout << "\nReading data from the file: \n\n";

    while(!iFile.eof()) {
        getline(iFile, str);
        cout << str << endl;
    }

    iFile.close();

    return 0;
}
content_copyCOPY

https://github.com/Internshala-Online-Trainings/programming-with-c-and-cpp-v2/blob/master/m4-diving-into-cpp-programming/m4-code challenges - solution/m4-t7-e4.cpp