Working with directory/folder and file in C++ Create, delete, show, rename

In this tutorial, we will learn about how to count the number of files in a directory, create file, delete file, search file, display. Let us see with some example and learn how to work with file and folder in C++.

What is directory  overview?

Directory operation to be supported include:

  • Search for a file
  • Create  a file – add to the directory
  • Delete a file  – erase from the directory
  • List a directory – possibly ordered in different ways.
  • Rename  a file  – may change sorting order
  • Traverse the file system

It is simple to implement, but each file must have a unique name.

Creating a Directory in C++

The first thing we need to create a directory/folder to save files in it.
Create a directory with some name like codespeedy and store it in dir.dname character variable.

cout<<"enter the directory name";
cin>>dir.dname;

Creating file and storing in the directory in C++

After creating a directory, we need to create the file. How to create a file, it is simple just create a file and store in the dir.fname [index]and dir.fcnt is index increment by 1. Like this, we can create file in directory.

cout<<"enter the file name ";
cin>>dir.fname[dir.fcnt];
dir.fcnt++;

Deleting a file from a directory in C++

How to delete a file, it is easy just search for a file in a directory, if the file is found it is deleted and next file is stored in the current index by using string copy. If the file is not found it is not deleted.

cout<<"enter the file name";
cin>>f;
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
cout<<"file "<<f<<" is deleted";
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
{
cout<<"file "<<f<<" is not found";
}
else
dir.fcnt--;

How to search for a file in a directory

It is simple, just given your input file it is stored in a variable say f, compare with the files in the directory if the file is found just print file f is found. If the file is not found print on to screen file f is not found.

cout<<"enter the file name";
cin>>f;
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
cout<<"file "<<f<<" is found";
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
{
cout<<"file "<<f<<" is not found";
}

How to display the file

If the index dir.fcnt is zero then, they are no files to display.
we will consider a for loop, to display elements.

cout<<"the file are";
for(i=0;i<dir.fcnt;i++)
cout<<"\n "<<dir.fname[i];

The code for display the file

if(dir.fcnt==0)
cout<<" directory are empty";
else
{
cout<<"the file are";
for(i=0;i<dir.fcnt;i++)
cout<<"\n "<<dir.fname[i];
}

How to count number of files in directory

We can count the number of files in a directory just by one line statement that is value of dir.fcnt.The value of dir.fcnt will count the number of files in the directory while we are creating a file into directory/folder.

cout<<" The number of files in directory are : "<<dir.fcnt;

Write a c++ program to implement files in the directory.

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
int  main()
{
int i,ch;
char f[30];
dir.fcnt=0;
cout<<"enter the directory name :";
cin>>dir.dname;
while(1)
{
cout<<"\n 1.create file\n 2.delete file \n 3.search \n 4.display \n 5.count \n 6.exit \n enter u r choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the file name ";
cin>>dir.fname[dir.fcnt];
dir.fcnt++;
break;
case 2:cout<<"enter the file name";
cin>>f;
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
cout<<"file "<<f<<" is deleted";
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
{
cout<<"file "<<f<<" is not found";
}
else
dir.fcnt--;
break;
case 3:cout<<"enter the file name";
cin>>f;
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
cout<<"file "<<f<<" is found";
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
{
cout<<"file "<<f<<" is not found";
}
break;
case 4:if(dir.fcnt==0)
cout<<" directory are empty";
else
{
cout<<"The file are";
for(i=0;i<dir.fcnt;i++)
cout<<"\n "<<dir.fname[i];
}
break;
case 5:cout<<" The number of files in directory are : "<<dir.fcnt;
break;
default: exit(0);
}
}
}

Output from Program

enter the directory name :codespeedy

 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 1
enter the file name  cat

 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 1
enter the file name dog

 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 1
enter the file name pig

 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 5
 The number of files in directory are : 3
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 2
enter the file name pig
file pig is deleted
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 5
 The number of files in directory are : 2
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 4
The file are
 cat
dog
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice3
enter the file name dog
file dog is found
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 5
 The number of files in directory are : 2
 1.create file
 2.delete file
 3.search
 4.display
 5.count
 6.exit
 enter u r choice 6
Process returned 0

Also, learn:

Leave a Reply

Your email address will not be published. Required fields are marked *