Print 1 to 100 in txt file using file handling.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream file;
file.open("E:\\BSCS.txt");
int i=1;
file<<"Number"<<endl;
while(i<=100)
{
file<<i<<endl;
i++;
}
file.close();
cout<<"File is created"<<endl;
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream file;
file.open("E:\\B.txt");
char var[100];
if(!file)
{
cout<<"Error .......file is not found.."<<endl;
exit(1);
}
while(!file.eof())
{
file>>var;
cout<<var<<" ";
}
cout<<endl;
file.close();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream file;
file.open("E:\\B.txt");
char var1;
int ch=0,no=0,other=0;
if(!file)
{
cout<<"Error .......file is not found.."<<endl;
exit(1);
}
int i=0;
while(!file.eof())
{
file.get(var1);
if(isalpha(var1))
{
ch++;
}
else if(isdigit(var1))
{
no++;
}
else
{
other++;
}
cout<<var1<<" ";
i++;
}
cout<<"\nTotal alphabets :"<<ch<<"\nTotal number :"<<no<<"\nOther :"<<other<<endl;
file.close();
return 0;
}
User defined function in C++ to count the number of blank present in a text file named "OUT.TXT".
void blankspace()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
count++;
}
cout<<"Number of blank spaces in file are "<<count;
fin.close();
}