Featured Post

Trie implementation in C

Basic File Operations in C++

Here's a simple implementation of basic File Operations in C++.
It includes :

1. Reading from a File
2. Writing to a File
3. Appending to a File
4. Deleting a File
5. Renaming a File
6. Listing Files in a directory

#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>

using namespace std;

class FileFunctions
{
private:
    string filename;

public:
    FileFunctions(string);
    void readFile();
    void writeFile();
    void appendFile();
    void copyFile();
    void renameFile();
    void deleteFile();
    void listFiles();
    ~FileFunctions();
};

FileFunctions::FileFunctions(string file)
{
    filename = file;
}

FileFunctions::~FileFunctions()
{

}

void FileFunctions::readFile()
{
    ifstream inf(filename.c_str());

    if (!inf)
    {
        cout << "Rec.txt could not be opened for reading!" << endl;
        return;
    }

    while (inf)
    {
        std::string strInput;
        getline(inf, strInput);
        cout << strInput << endl;
    }
    inf.close();
}

void FileFunctions::writeFile()
{
    ofstream outf(filename.c_str());

    if (!outf)
    {
        cout << "Rec.txt could not be opened for writing!" << endl;
        return;
    }

    outf << "I am a C++ Programmer ." << endl;
    outf << "I like C also ." << endl;
    outf.close();
}

void FileFunctions::appendFile()
{
    ofstream outf(filename.c_str(),ios::app);

    if (!outf)
    {
        cout << "Rec.txt could not be opened for writing!" << endl;
        exit(1);
    }

    outf << "I love algorithms . " << endl;
    outf << "I play with data structures." << endl;

    outf.close();
}

void FileFunctions::deleteFile()
{
    char ch;
    string path;

    cout<<"Enter the complete path for the file to be deleted : ";
    cin>>path;

    ifstream infile(path.c_str());

    if(!infile)
    {
        cout<<"File doesn't exists !! "<<endl;
        return;
    }

    cout<<"Do you really want to delete \""<<path<<"\" (Y/N)? "<<endl;
    cin>>ch;
    if(ch == 'y' || ch == 'Y')
    {
        remove(path.c_str());
        cout<<"File Deleted..."<<endl;
    }
}
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL)
    {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL)
    {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
}

void FileFunctions::listFiles()
{
    string dir = string(".");
    vector<string> files = vector<string>();

    getdir(dir,files);

    cout<<"\nListing files in the current Directory....\n\n";

    for (unsigned int i = 0; i < files.size(); i++)
    {
        cout << files[i] << endl;
    }

}

void FileFunctions::renameFile()
{
  string oldFileName,newFileName;

  cout<<"\nEnter the file name to be renamed : ";
  cin>>oldFileName;
  cout<<"\nEnter the new file name : ";
  cin>>newFileName;
    
  ifstream infile(oldFileName.c_str());

    if(!infile)
    {
        cout<<"File doesn't exists !! "<<endl;
        return;
    }

  else
  {
    rename(oldFileName.c_str(),newFileName.c_str());
    cout<<"File \""<<oldFileName<<"\" renamed to \""<<newFileName<<"\"\n";
  }

}
int main()
{
    FileFunctions f("Rec.txt");

    f.readFile();
    f.writeFile();
    f.readFile();
    f.appendFile();
    f.readFile();
    f.deleteFile();
    f.listFiles();
    f.renameFile();
    return 0;
}

Comments