Basic Operation On Text File In C++ download

File I/O is a five-step process:
1. Include the header file fstream in the program.
2. Declare file stream object.
3. Open the file with the file stream object.
4. Use the file stream object with >>, <<, or other input/output functions.
5. Close the files.

Following program shows how the steps might appear in program.

 

Program to write in a text file


#include <fstream>
using namespace std;

int main()
{
    ofstream fout;
    fout.open("out.txt");
    
    char str[300] = "Time is a great teacher but 
            unfortunately it kills all its pupils. Berlioz";

    //Write string to the file.
    fout << str;

    fout.close();
    return 0;
}

Program to read from text file and display it


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

int main()
{
    ifstream fin;
    fin.open("out.txt");
    
    char ch;
    
    while(!fin.eof())
    {
         fin.get(ch);
         cout << ch;
    }
    
    fin.close();
    return 0;
}
 

Program to count number of characters.


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

int main()
{
    ifstream fin;
    fin.open("out.txt");

    int count = 0;
    char ch; 

    while(!fin.eof())
    {
        fin.get(ch);
        count++;
    }
    
    cout << "Number of characters in file are " << count;
    
    fin.close();
    return 0;
}

Program to count number of words


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

int main()
{
    ifstream fin;
    fin.open("out.txt");

    int count = 0;
    char word[30]; 

    while(!fin.eof())
    {
        fin >> word;
        count++;
    }
    
    cout << "Number of words in file are " << count;
    
    fin.close();
    return 0;
}

Program to count number of lines


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

int main()
{
    ifstream fin;
    fin.open("out.txt");

    int count = 0;
    char str[80];
    
    while(!fin.eof())
    {
        fin.getline(str,80);
        count++;
    }
    
    cout << "Number of lines in file are " << count;
  
    fin.close();
    return 0;
}

 

Program to copy contents of file to another file.


#include<fstream>
using namespace std;

int main()
{
    ifstream fin;
    fin.open("out.txt");
    
    ofstream fout;
    fout.open("sample.txt");
    
    char ch;
    
    while(!fin.eof())
    {
        fin.get(ch);
        fout << ch;
    }

    fin.close();
    fout.close();
    return 0;
}

Basic Operation On Binary File In C++

When data is stored in a file in the binary format, reading and writing
data is faster because no time is lost in converting the data from one format to another format. Such files are called binary files. This following program explains how to create binary files and also how to read, write, search, delete and modify data from binary files.

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

class Student
{
    int admno;
    char name[50];
public:
    void setData()
    {
        cout << "\nEnter admission no. ";
        cin >> admno;
        cout << "Enter name of student ";
        cin.getline(name,50);
    }

    void showData()
    {
        cout << "\nAdmission no. : " << admno;
        cout << "\nStudent Name : " << name;
    }
	 
    int retAdmno()
    {
        return admno;
    }
};

/*
* function to write in a binary file.
*/

void write_record()
{
    ofstream outFile;
    outFile.open("student.dat", ios::binary | ios::app);

    Student obj;
    obj.setData();
    
    outFile.write((char*)&obj, sizeof(obj));
    
    outFile.close();
}

/*
* function to display records of file
*/


void display()
{
    ifstream inFile;
    inFile.open("student.dat", ios::binary);

    Student obj;
    
    while(inFile.read((char*)&obj, sizeof(obj)))
    {
        obj.showData();
    }        
    
    inFile.close();
}

/*
* function to search and display from binary file
*/

void search(int n)
{
    ifstream inFile;
    inFile.open("student.dat", ios::binary);
    
    Student obj;

    while(inFile.read((char*)&obj, sizeof(obj)))
    {
        if(obj.retAdmno() == n)
        {
            obj.showData();
        }
    }
    
    inFile.close();
}

/*
* function to delete a record
*/

void delete_record(int n)
{
    Student obj;
    ifstream inFile;
    inFile.open("student.dat", ios::binary);

    ofstream outFile;
    outFile.open("temp.dat", ios::out | ios::binary);
    
    while(inFile.read((char*)&obj, sizeof(obj)))
    {
        if(obj.retAdmno() != n)
        {
            outFile.write((char*)&obj, sizeof(obj));
        }
    }

    inFile.close();
    outFile.close();
    
    remove("student.dat");
    rename("temp.dat", "student.dat");
}

/*
* function to modify a record
*/

void modify_record(int n)
{
    fstream file;
    file.open("student.dat",ios::in | ios::out);

    Student obj;

    while(file.read((char*)&obj, sizeof(obj)))
    {
        if(obj.retAdmno() == n)
        {
            cout << "\nEnter the new details of student";
            obj.setData();
            
            int pos = -1 * sizeof(obj);
            file.seekp(pos, ios::cur);
			
            file.write((char*)&obj, sizeof(obj));
        }
    }
  
    file.close();
}

int main()
{
    //Store 4 records in file
    for(int i = 1; i <= 4; i++)
       write_record();
	   
    //Display all records
    cout << "\nList of records";
    display();
	
    //Search record
    cout << "\nSearch result";
    search(100);
	
    //Delete record 
    delete_record(100);
    cout << "\nRecord Deleted";
	
    //Modify record
    cout << "\nModify Record 101 ";
    modify_record(101);
	
    return 0;
}