Tuesday, October 21, 2014

File Handling using C++

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
class file
{
    private: FILE *fp;
    public:
                file(){}
                file(const char*name,const char*mode)
                {
                    fp=fopen(name,mode);

                    if(fp==NULL)
                    {
                        cout<<"\n Can not open file "<<name;
                        exit(1);
                    }
                }
                ~file()
                {
                    fclose(fp);
                }
                FILE *getp()
                {
                    return fp;
                }
};
void main()
{
    char filename[1000],ch;
    cout<<"\n Enter the name of the filename=";
    cin>>filename;
    file f(filename,"r");
    while((ch==fgetc(f.getp()))!=EOF)
    cout<<ch;
    getch();
}

No comments:

Post a Comment