Professional Documents
Culture Documents
#include <fstream>
int main()
{
fout<<"Hi";
fout.close();
return 0;
}
===
#include<fstream>
#include<iostream>
using namespace std;
main()
{
ifstream fin ("data.txt");
string str;
getline(fin,str);
cout<<str;
fin.close();
}======
#include <iostream>
#include <fstream>
fout.close();
return 0;
}
=====
#include<fstream>
#include<iostream>
using namespace std;
main()
{
string str;
ofstream fout ("data.txt"); // writing
getline(cin,str);
fout<<str;
fout.close();
===
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string name,ccode;
int reg;
fout.close();
return 0;
}
====
#include<fstream>
#include<iostream>
using namespace std;
main()
{
string name,n;
int age,a;
double cgpa,c;
ofstream fout ("data.txt"); // writing
getline(cin,name);
cin>>age;
cin>>cgpa;
fout<<name<<endl;
fout<<age<<endl;
fout<<cgpa;
fout.close();
ifstream fin ("data.txt"); // reading
getline(fin,n);
fin>>a;
fin>>c;
fin.close();
=====
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
string str;
====
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char ch;
fin.close();
====
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
char ch[20];
ifstream fin ;
fin.open("data.txt");
fin.close();
}
===
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("country.txt");
fout<<"India\n";
fout<<"USA\n";
fout<<"UK\n";
fout.close();
fout.open("capital.txt");
fout<<"Delhi\n";
fout<<"Washington\n";
fout<<"Londan\n";
fout.close();
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("country.txt");
fout<<"India\n";
fout<<"USA\n";
fout<<"UK\n";
fout.close();
fout.open("capital.txt");
fout<<"Delhi\n";
fout<<"Washington\n";
fout<<"Londan\n";
fout.close();
ofstream fout;
fout.open("country.txt",ios::out | ios::trunc);
or
======
// Append (add data in the end)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("country.txt",ios::out | ios::app);
fout<<"India\n";
fout<<"USA\n";
fout<<"UK\n";
fout.close();
}
===
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("country.txt",ios::app);
fout<<"India\n";
fout<<"USA\n";
fout<<"UK\n";
fout.close();
}
====
// concatenate
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("country.txt",ios::ate);
fout<<"India\n";
fout<<"US\n";
//fout<<"UK\n";
fout.close();
}
====
===
/*
WAP to read a text file using char method and count
-number of characters
-number of words
-number of lines
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c ;
int w=0, l=1;
int count=0;
ifstream fin("data.txt");
if(c==' ')
w++;
else if (c==13)
l++;
count++ ;
}
fin.close();
return 0 ;
}
Or
/*
WAP to read a text file using char method and count
-number of characters
-number of words
-number of lines
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char c ;
int w=0, l=0;
int count=0;
ifstream fin("data.txt");
while ( !fin.eof()) //while (fin)
{
fin.get(c);
cout<<c;
if(c==' ')
w++;
else if (c=='\n')
{ l++; w++; }
else if(fin.eof())
{ l++; w++; }
else
count++ ;
}
fin.close();
return 0 ;
}