0% found this document useful (0 votes)
4 views3 pages

OS Lab Exp 4

The document contains four C programming examples related to directory operations. Example 1 demonstrates how to create a directory, Example 2 lists the contents of the current directory, Example 3 shows how to delete a directory, and Example 4 retrieves and displays the current working directory. Each example includes error handling for various operations.

Uploaded by

ahmadsalim8470
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)
4 views3 pages

OS Lab Exp 4

The document contains four C programming examples related to directory operations. Example 1 demonstrates how to create a directory, Example 2 lists the contents of the current directory, Example 3 shows how to delete a directory, and Example 4 retrieves and displays the current working directory. Each example includes error handling for various operations.

Uploaded by

ahmadsalim8470
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/ 3

EXP – 4

EX – 1

#include <stdio.h>

#include <sys/stat.h>
#include <sys/types.h>
int main() {
char dirname[256];
printf("Enter directory name: ");

scanf("%s", dirname);
if (mkdir(dirname, 0755) == 0) {
printf("Directory created successfully.\n");
} else {
perror("Error creating directory");

}
return 0;
}

EX – 2

#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir = opendir(".");
if (dir == NULL) {

perror("Error opening directory");


return 1;
}

struct dirent *entry;

printf("Directory contents:\n");
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

closedir(dir);
return 0;
}

EX – 3

#include <stdio.h>
#include <unistd.h>
int main() {
char dirname[256];
printf("Enter directory name to delete: ");

scanf("%s", dirname);
if (rmdir(dirname) == 0) {
printf("Directory deleted successfully.\n");
} else {
perror("Error deleting directory");

}
return 0;
}

EX – 4
#include <stdio.h>
#include <unistd.h>

int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);

} else {
perror("Error retrieving current directory");
}
return 0;
}

You might also like