-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1a_Single_Level.c
96 lines (87 loc) · 3.17 KB
/
1a_Single_Level.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct directory {
char dirName[10];
char fileName[10][10]; /* fileName[10][i] -> i Array of 10 char */
int fileCount;
} dir;
void main()
{
int i, ch;
char tmp[10];
char tempFileName[10];
dir.fileCount = 0;
printf("Enter a directory name:");
scanf("%s", dir.dirName);
while (1)
{
printf("\nOperations\n\n1.Create File\n2.Delete File\n3.Search in Directory\n4.View Files\n5.Exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the name of the file:");
scanf("%s", tempFileName);
//check if file already exists
for (i = 0; i < dir.fileCount; i++)
{
if (strcmp(dir.fileName[i], tempFileName) == 0)
{
printf("File already exists\n");
break;
}
}
//if file does not exist
if(i == dir.fileCount)
{
strcpy(dir.fileName[dir.fileCount++], tempFileName);
printf("File created successfully\n");
}
break;
case 2:
printf("Enter the name of the file:");
scanf("%s", tmp);
for (i = 0; i < dir.fileCount; ++i) {
if (strcmp(tmp, dir.fileName[i]) == 0) { /* The strcmp() function takes two strings and return an integer. 0->identical */
printf("The File %s is deleted!\n", tmp); /* Copy the last element to the place of deleted one */
strcpy(dir.fileName[i], dir.fileName[dir.fileCount - 1]); /* The strcpy() function copies the string to the another character array. strcpy(destination, source) */
dir.fileCount--;
break;
}
}
if (i == dir.fileCount) {
printf("404 | File Not Found\n");
}
break;
case 3:
printf("Enter the name of the file to be searched for:");
scanf("%s", tmp);
for (i = 0; i < dir.fileCount; ++i) {
if (strcmp(tmp, dir.fileName[i]) == 0) {
printf("File Found!!");
break;
}
}
if (i == dir.fileCount) {
printf("404 | File Not Found");
}
break;
case 4:
if (dir.fileCount == 0) {
printf("Empty Directory!!");
}
else {
printf("Files:\n");
for (i = 0; i < dir.fileCount; ++i)
{
printf("%s\n", dir.fileName[i]);
}
printf("Total %d files in 1 directory", dir.fileCount);
}
break;
default:
exit(0);
}
}
}