To create a file in c++ and write data to it it can be done as follows
#include <fstream>
#include <iostream>
#include<string>
using namespace std;
int main()
{
ofstream fout;
//Creating the file sample.txt
fout.open("sample.txt");
if(fout.is_open())
{
//writing content to the file
fout<<"Hello Good Day";
}
//Reading from the file
ifstream fin;
fin.open("sample.txt");
if(fin.is_open())
{
string line;
//reading from the file
while(getline(fin,line))
{
cout<<line;
}
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.