You are on page 1of 11

//#include <iostream>

#include <fstream>

using namespace std;

int main()
{

ofstream fout ("data.txt");

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>

using namespace std;


int main()
{
string str, filename;

cout<<" enter the content you want write in file: ";


getline(cin,str);

cout<<"\nEnter a valid file name: ";


cin>>filename;

ofstream fout (filename);


fout<<str;

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();

        ifstream fin ("data.txt");   // reading


        getline(fin,str);
        cout<<str;
        fin.close();
}

===
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
string name,ccode;
int reg;

cout<<" enter the content you want write in file: ";

cout<<"\n Enter your name: ";


getline(cin,name);

cout<<"\n Your reg, number: ";


cin>>reg;
fflush(stdin);
cout<<"\n enter the course code: ";
getline(cin,ccode);

ofstream fout ("file1.txt");


fout<<name<<endl;
fout<<reg<<endl;
fout<<ccode;

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;

cout<<"\n Name = "<< n;


cout<<"\n Age  = "<< a;
cout<<"\n CGPA = "<< c;

        fin.close();

=====

#include<fstream>
#include<iostream>
using namespace std;

int main()
{

string str;

ifstream fin ("data.txt");

for (int i=0; !fin.eof() ; i++)


{
getline(fin,str);
cout<< str<<endl;
}
fin.close();

====

#include<fstream>
#include<iostream>
using namespace std;

int main()
{

char ch;

ifstream fin ("data.txt");

for (int i=0; !fin.eof() ; i++)


{
fin>>ch;
cout<<ch;
}

fin.close();

====

#include<fstream>
#include<iostream>
using namespace std;

int main()
{

char ch[20];

ifstream fin ;
fin.open("data.txt");

for (int i=0; !fin.eof() ; i++)


{
fin.getline(ch,1);
cout<<ch;
}

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();

    const int n=80;


     char s[n];
        ifstream fin;
        fin.open("country.txt");
        while(fin)  //!fin.eof()
     {
       fin.getline(s,n);    // getline(fin,s); but s should be string object.
       cout<<s<<endl;;
     }
        fin.close();
        
        fin.open("capital.txt");
        while(fin)
     {
       fin.getline(s,n);
       cout<<s<<endl;
     }
        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();

    const int n=80;


     char s[n];
        ifstream fin, fin2;
        fin.open("country.txt");
        fin2.open("capital.txt");
        
        while(fin || fin2)  //!fin.eof()
     {
       fin.getline(s,n);    // getline(fin,s); but s should be string object.
       cout<<s<<" : ";
       fin2.getline(s,n);
       cout<<s<<endl;
     }
        fin.close();
        fin2.close();
}
====

ofstream fout;
fout.open("country.txt",ios::out | ios::trunc);

this equivalent to 

ofstream fout;       fout.open("country.txt");

or

ofstream fout ("country.txt");

        ======
        
        // 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();
}

===

// Append (add data in the end)

#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();
}

====

// will not create a new file


#include <iostream>
#include <fstream>
using namespace std;
int main()
{
        ofstream fout;
        fout.open("country.txt",ios::out | ios::nocreate);
        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");

while ( !fin.eof()) //while (fin)


{
fin.get(c);
cout<<c;

if(c==' ')
w++;
else if (c==13)
l++;

count++ ;
}

cout<<"\n words = "<<w;


cout<<"\n Lines = "<<l;
cout<<"\n characters = "<<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++ ;
}

cout<<"\n words = "<<w;


cout<<"\n Lines = "<<l;
cout<<"\n characters = "<<count;

fin.close();
return 0 ;
}

        

You might also like