Embed presentation
Download to read offline
![1
Q: read and write data froma file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char arr[20];
char arr2[20];
for(int i=0;i<20;i++)
{
cin>>arr[i];
}
ofstream fp;
fp.open("filehandling.txt");
fp<<arr;
fp.close();
cout<<"reading from file"<<endl;
ifstream iff("filehandling.txt");
iff>>arr2;](https://image.slidesharecdn.com/oopass2-180909193258/75/file-handling-1-2048.jpg)
![2
for(int i=0;i<20;i++)
{
cout<<arr2[i];
}
return 0;
}](https://image.slidesharecdn.com/oopass2-180909193258/75/file-handling-2-2048.jpg)
This code allows a user to read and write data to a file. It takes in 20 characters from the user and stores them in an array. It then opens a file called "filehandling.txt" and writes the array to the file. It closes the file and then reopens it to read the data back into a second array, printing the contents to the screen. This demonstrates basic file input/output operations in C++.
![1
Q: read and write data froma file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char arr[20];
char arr2[20];
for(int i=0;i<20;i++)
{
cin>>arr[i];
}
ofstream fp;
fp.open("filehandling.txt");
fp<<arr;
fp.close();
cout<<"reading from file"<<endl;
ifstream iff("filehandling.txt");
iff>>arr2;](https://image.slidesharecdn.com/oopass2-180909193258/75/file-handling-1-2048.jpg)
![2
for(int i=0;i<20;i++)
{
cout<<arr2[i];
}
return 0;
}](https://image.slidesharecdn.com/oopass2-180909193258/75/file-handling-2-2048.jpg)