Lab7 Os
Lab7 Os
#include <stdio.h>
//#include<conio.h>
#include<string.h>
#include <stdlib.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
int main()
{
int i,ch;
char f[30];
dir.fcnt==0;
printf("\nEnter name of directory: ");
scanf("%s",dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File\t4. Display Files\t5. Exit\nEnter
your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the name of the file: ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2:
printf("\nEnter the name of the file: ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
printf("\nFile %s is deleted",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("\nFile %s not found",f);
else
dir.fcnt--;
break;
case 3:
printf("\nEnter the name of the file: ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f,dir.fname[i])==0)
{
printf("\nFile %s is found",f);
break;
}
}
if(i==dir.fcnt)
printf("\nFile %s not found",f);
break;
case 4:
if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are: ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default:
exit(0);
}
}
getch();
}
7b
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_DIR 10
#define MAX_FILES 10
#define MAX_NAME_LENGTH 30
struct Directory {
char dname[MAX_NAME_LENGTH];
char fname[MAX_FILES][MAX_NAME_LENGTH];
int fcnt;
};
int main() {
int i, ch, dcnt = 0, k;
char f[MAX_NAME_LENGTH], d[MAX_NAME_LENGTH];
struct Directory dir[MAX_DIR];
while (1) {
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t5. Display\t6. Exit\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
if (dcnt >= MAX_DIR) {
printf("\nMaximum number of directories reached.\n");
break;
}
printf("\nEnter name of directory: ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("\nDirectory created\n");
break;
case 2:
printf("\nEnter name of the directory: ");
scanf("%s", d);
i = findDirectory(dir, dcnt, d);
if (i != -1) {
if (dir[i].fcnt >= MAX_FILES) {
printf("\nMaximum number of files in directory reached.\n");
break;
}
printf("\nEnter name of the file: ");
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("\nFile created\n");
} else {
printf("\nDirectory %s not found\n", d);
}
break;
case 3:
printf("\nEnter name of the directory: ");
scanf("%s", d);
k = findFile(dir[i], f);
if (k != -1) {
printf("\nFile %s is deleted\n", f);
dir[i].fcnt--;
// Move the last file to the deleted file's position
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
} else {
printf("\nFile %s not found\n", f);
}
} else {
printf("\nDirectory %s not found\n", d);
}
break;
case 4:
printf("\nEnter name of the directory: ");
scanf("%s", d);
k = findFile(dir[i], f);
if (k != -1) {
printf("\nFile %s is found\n", f);
} else {
printf("\nFile %s not found\n", f);
}
} else {
printf("\nDirectory %s not found\n", d);
}
break;
case 5:
if (dcnt == 0) {
printf("\nNo Directories\n");
} else {
printf("\nDirectory\tFiles\n");
for (i = 0; i < dcnt; i++) {
printf("%s\t", dir[i].dname);
for (k = 0; k < dir[i].fcnt; k++) {
printf("%s\t", dir[i].fname[k]);
}
printf("\n");
}
}
break;
case 6:
exit(0);
default:
printf("\nInvalid choice. Please enter a valid option.\n");
break;
}
}
return 0;
}