Shalvin Interests

Sunday, December 27, 2015

C++ File Handling

Program to create a file and put value to it 

The header file for C++ file handling is fstram.h. We are making use of Output stream (ofstream) for outputting data from Program to file. Here the open method of ofstream class is used for opening the file. It is also possible to use the constructor of ofstream class.


#include <iostream.h>
#include <fstream.h>

int main()
{
    ofstream flout;
    flout.open("marks.dat", ios::out);
    char ans = 'y';
    int rollno;
    float marks;
    while(ans == 'y' || ans == 'Y')
    {
        cout<<"\n Enter Rollno. : ";
        cin>>rollno;
        cout<<"\n Enter Points :";
        cin>>points;
        flout<<rollno<<'\n'<<points<<'\n';
        cout<<"\n Want to enter more records?(y/n)..";
        cin>>ans;

    }
    flout.close();
    return 0;

}


The text file will be created in the bin folder.




Program to input details of 5 students and display them
This progam makes use of ofstream and ifstream classes.



#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    ofstream fout("student", ios::out);
    char name[30], ch;
    float marks = 0.0;

    //Loop to get 5 records
    for(int i = 0; i<5;i++)
    {
        cout<<"Student"<<(i+1)<<":\tName:";
        cin.get(name, 30);
        cout<<"\t\tMarks:";
        cin>>marks;
        cin.get(ch);
        fout<<name<<'\n'<<marks<<'\n';

    }
    fout.close();
    ifstream fin("student", ios::in);
    fin.seekg(0);
    cout<<"\n";

    for(i=0;i<5;i++)
    {
        fin.get(name, 30);
        fin.get(ch);
        fin>>marks;
        fin.get(ch);
        cout<<"Student Name :"<<name;
        cout<<"\tMarks :"<<marks<<"\n";
    }
    fin.close();
    return 0;
}

Writing to and reading from multiple files in succession

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int main()
{
    system("cls");
    //Create two files
    ofstream fileout;
    fileout.open("Stunames", ios::out);
    fileout<<"Arundhati \n"<<"Lakshmi \n"<<"Shalvin \\";
    fileout.close();
    fileout.open("stumarks", ios::out);
    fileout<<"100\n"<<"100\n"<<"95\n";
    fileout.close();

    //Read these files
    char line[80];
    ifstream filin;
    filin.open("stunames", ios::in);
    cout<<"The contents of studnames are given below\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.close();

    filin.open("stumarks", ios::in);
    cout<<"\nThe contents of stumarks file are given below\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.getline(line, 80);
    cout<<line<<"\n";
    filin.close();
    return 0;
}

Display contents of a file using get() function
get() function read a byte of data.


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    char ch;
    ifstream fin;
    fin.open("marks.dat",  ios::in);
    if(!fin)
    {
        cout<<"Cannot open file !!\n";
        return 1;
    }
    while(fin)
    {
        fin.get(ch);
        cout<<ch;
    }
    fin.close();
    return 0;

}

When end-of-file is reached, the stream associated  with the file becomes zero.

Output



Create a file using put() function
put() function will also write one byte at a time

The following program creates a ASCII chart of printable characters and displays them in 10 column format.


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
    system("cls");
    ofstream fout;
    fout.open("Aschars", ios::app);
    if(!fout)
    {
        cout<<"the file cannot be opened!!\n";
        return 1;
    }
    char ch;
    int line = 0;
    for(int i = 33;i<128;i++)
    {
        cout.put(((char)i));
    }
    fout.close();

    ifstream fin;
    fin.open("Aschars", ios::in);
    fin.seekg(0);
    for(i = 33;i<129;i++)
    {
        fin.get(ch);
        cout<<"  "<<i<<"=";
        cout.put((char)(i));
        if(!(i%10))
        {
            cout<<endl;line++;
        }
        if(line >> 22)
        {
            system("PAUSE");
            line = 0;
        }
    }
    return 0;
}

read() and write() functions for working reading and writing entire structure

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
struct customer
{
    char name[51];
    float balance;

};

int main()
{
    system("cls");
    customer savac;
    strcpy(savac.name, "Shalvin P D");
    savac.balance = 10500.50;
    ofstream fout;
    fout.open("Saving", ios::out|ios::binary);
    if(!fout)
    {
        cout<<"File cannot be openened\n";
        return 1;
    }
    fout.write((char *) &savac, sizeof(customer));
    fout.close();

    ifstream fin;
    fin.open("Saving", ios::in|ios::binary);
    fin.read((char *) &savac, sizeof(customer));
    cout<<savac.name;
    cout<<" has the balance amount of Rs. "<<savac.balance<<"\n";
    fin.close();
    return 0;
}

Output