Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views2 pages

Simulate File Directory Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Simulate File Directory Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 8: Simulate following File Organization Techniques:

a) Single level directory b) Two level directory

#include <stdio.h>
#include <string.h>
#define MAX_FILES 100
#define MAX 20

// Structure to represent a file


typedef struct
{
char name[MAX];
int index;
} File;

// Function prototypes
void singleLevelDirectory();
void twoLevelDirectory();

int main()
{
printf("Simulating Single Level Directory\n");
singleLevelDirectory();
printf("\nSimulating Two Level Directory\n");
twoLevelDirectory();
return 0;
}

// Function to simulate single level directory


void singleLevelDirectory()
{
File files[MAX_FILES];
int count = 0;
// Adding files to the directory
char buf[MAX];
printf("Enter the file name:\n");
scanf("%s", buf);
strcpy(files[count].name, buf);
files[count++].index = 0;
printf("Enter the file name:\n");
scanf("%s", buf);
strcpy(files[count].name, buf);
files[count++].index = 0;
// Listing files
printf("Files in the directory are:\n");
for (int i = 0; i < count; i++)
{
printf("\t%s\n", files[i].name);
}
return;
}

// Function to simulate two level directory


void twoLevelDirectory()
{
File files[MAX_FILES];
int count = 0;
char buf[MAX];
// Adding files to directories
printf("Enter the file name:\n");
scanf("%s", buf);
strcpy(files[count].name, buf);
files[count++].index = 0; // Directory 1
printf("Enter the file name:\n");
scanf("%s", buf);
strcpy(files[count].name, buf);
files[count++].index = 0; // Directory 1
printf("Enter the file name:\n");
scanf("%s", buf);
strcpy(files[count].name, buf);
files[count++].index = 1; // Directory 2
// Listing files in directories
printf("Files in directory 1 are:\n");
for (int i = 0; i < count; i++)
{
if (files[i].index == 0)
{
printf("\t%s\n", files[i].name);
}
}
printf("\nFiles in directory 2 are:\n");
for (int i = 0; i < count; i++)
{
if (files[i].index == 1)
{
printf("\t%s\n", files[i].name);
}
}
return;
}

You might also like