Write Integers into The File
Write a code to accept two integers from the user and write them into the file.
//writing two integers into the file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int num1,num2;
// create a file and open it in the write mode
ofstream fout("c:\\compuhelp\\first.txt");
//fout.open("c:\\compuhelp\\first.txt");
cout<<
"Enter first number ";
cin>>num1;
cout<<
"Enter second number ";
cin>>num2;
fout<<num1<<endl<<num2;
cout<<"Data saved ";
fout.close();
}
Write a code to update a file using append (ios::app) mode of fstream class.
//writing two integers into the file in append mode
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int num1,num2;
// Open the file in append mode
fstream fout("c:\\compuhelp\\first.txt",ios::app);
//fout.open("c:\\compuhelp\\first.txt");
cout<<
"Enter first number ";
cin>>num1;
cout<<
"Enter second number ";
cin>>num2;
fout<<endl<<num1<<endl<<num2;
cout<<"File updated";
fout.close();
}