file handling in c++
Files are used to store data permanently in a storage devices.
File handling is a mechanism to store output of program in a file.
Two methods of file handing
Read data from file
Write operation
To use these function we must include stream file in our program
write operation:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i;
fstream file;
file.open("wakey.txt",ios::out);
for(int i=1;i<=10;i++)
{
file<<20<<"*"<<i<<"="<<20*i<<endl;
}
return 0;
}
output:
text type file in created in your computer
20*1=20
20*2=40
20*3=60
20*4=80
20*5=100
20*6=120
20*7=140
20*8=160
20*9=180
20*10=200
write operation video:
read operation in file handling:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int i;
fstream file;
file.open("wakey.txt",ios::in);
if(!file)
{
cout<<"file no exist";
}
while(1)
{
char ch;
file>>ch;
if(file.eof())
break;
cout<<ch;
}
return 0;
}
output:
this time it read data from file and show output
20*1=20
20*2=40
20*3=60
20*4=80
20*5=100
20*6=120
20*7=140
20*8=160
20*9=180
20*10=200
video lecture:
No comments:
Post a Comment