0% found this document useful (0 votes)
34 views

File Management System (Assignment No 4)

The document describes a C++ program that implements file management operations like create, delete, and view files using an array-based linked list data structure. The program defines File and FileData structures to store file metadata and content. A FileManagement class contains methods to add a new file, delete an existing file, and view a file's content based on its name and extension. The main function contains a menu loop that allows the user to select and test the file management operations.

Uploaded by

Hamza Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

File Management System (Assignment No 4)

The document describes a C++ program that implements file management operations like create, delete, and view files using an array-based linked list data structure. The program defines File and FileData structures to store file metadata and content. A FileManagement class contains methods to add a new file, delete an existing file, and view a file's content based on its name and extension. The main function contains a menu loop that allows the user to select and test the file management operations.

Uploaded by

Hamza Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Amna Zarish

Reg Id: SP19-BSE-026

Question No 1
Develop a program for managing files (details discussed in the first lecture on
array based link list) using array based linked list.
Operations
Create file
Delete file
View file
Answer

#include<iostream>
#include <string.h>
using namespace std;

struct File
{
string file_name,file_ext;
int temp;
int file_index,starting_point;
};
struct FileData
{
int next_index;
char ch;

};

class FileManagement
{
private:
FileData fileData[150];
File file_info[100];
public:
FileManagement()
{ int x,y = 0;
while(x<150)
{
fileData[x].next_index=-1;
x++;
}
fileData[150].next_index=-3;
while( y<100)
{
file_info[y].file_index=-1;
y++;
}
file_info[99].file_index=-3;
}
Void new_file(string first_name,string Ext,char filedata[])
{
int x,y,z;
int i=0;
int j=0;
int check=0;
int length=strlen(filedata);
while(fileData[x].next_index!=-1)
{
x++;
}
cout<<"\n";
while(file_info[y].file_index!=-1)
{
y++;

}
file_info[y].starting_point=x;
file_info[y].file_index=y;
file_info[y].file_name=first_name;
file_info[y].file_ext=Ext;
i=x;
while(j<length)
{
fileData[i].ch=filedata[z];
fileData[i].next_index=i;
i++;
j++;
z++;
}
fileData[i].next_index=-2;

void viewFile(string first_name,string ext)


{
int i=0;
int temp=-1;
int k=0 ;
while(file_info[i].file_index!=-1)
{
if((file_info[i].file_name==first_name)&&
(file_info[i].file_ext==ext))
{
temp=file_info[i].temp;
}
i++;
}
if(temp!=-1)
{
cout<<"ch:\n";
for(int w = temp ; fileData[w].next_index!=-2;w++)
{
cout<<fileData[fileData[w].next_index].ch<<"";
}
cout<<"\nFile Viewed Successfully";
}
else
{
cout<<"File Not Found";
}
}
bool delFile(string first_name,string ext)
{
int i=0;
int temp=-1;
int k=0 ;
while(file_info[i].file_index!=-1)
{
if((file_info[i].file_name==first_name)&&
(file_info[i].file_ext==ext))
{
temp=file_info[i].temp;
file_info[i].file_index=-1;
}
i++;
}
if(temp!=-1)
{
for(int w = temp ; fileData[w].next_index!=-2;w++)
{
fileData[w].next_index=-1;
}
return true;
}
else
{
return false;
}

};
int main()
{
int w, option = 1;
FileManagement file;
string file_name,Ext;
char fileData[150];

while (option != 0)
{
cout << "\n\n Press 1 to Create a file: ";
cout << "\n\n Press 2 to Delete File. ";
cout << "\n\n Press 3 to View File ";
cout << "\n\n Press 0 to Exit. ";
cout << "\n\n Enter Your Choice... ";
cin >> option;

if(option == 1){
cout<<"Enter File Name\n";
cin>>file_name;
cout<<"Enter File Extension\n";
cin>>Ext;
cout<<"Enter File Data\n";
cin>>fileData;
file.new_file(file_name,Ext,fileData);
}
if(option == 2)
{
cout<<"Enter File Name\n";
cin>>file_name;
cout<<"Enter File Extension\n";
cin>>Ext;
if(file.delFile(file_name,Ext))
{
cout<<"File Deleted Succesfully";
}
else
{
cout<<"File not found: File Deletion Unsucessful";
}

}
if(option == 3)
{
cout<<"Enter File Name\n";
cin>>file_name;
cout<<"Enter File Extension\n";
cin>>Ext;
file.viewFile(file_name,Ext);

}
}

You might also like