Create the following subdirectory structure under your home directory using Program 1.
Subdir1
-------------------------------------------------------------------------------------------------
| | |
D1 d2 d3
-------------------------------- -------------------------------------- --------------------------------------
| | | | | | | | |
A1.txt A2.txt A3.txt B1.ccc B2.ccc B3.ccc C1.txt C2.ddd C3.txt
Create a C function with the following capabilities:
Program 1:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
void main() {
char path1 = "/home/Subdir1"; // path for subdirectory under your home directory
int res;
res = mkdir(path1, 0777); // create the directory using mkdir function
if(!res) {
printf("Directory created successfully");
}
else {
printf("Error in creating directory");
exit(1);
}
//creating subdirectors D1, d2, and d3 under Subdir1 with permision 0777
char path2 = "/home/Subdir1/D1";
char path3 = "/home/Subdir1/d2";
char path4 = "/home/Subdir1/d3";
res2 = mkdir(path2, 0777);
res3 = mkdir(path3, 0777);
res3 = mkdir(path4, 0777);
FILE * fptr; // create a file pointer
char str[] = "abcdefghij";
fptr = fopen("/home/Subdir1/D1/A1.txt", "w", 0666); // open the file A1.txt in write mode with 0666 permission
for(i=0;str[i]!='\n';i++){
fputc(str[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
char str1[] = "you are beautiful";
fptr = fopen("/home/Subdir1/D1/A2.txt", "w", 0666); // open the file A2.txt in write mode with 0666 permission
for(i=0;str1[i]!='\n';i++){
fputc(str1[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
char str2[] = "this is terrible!";
fptr = fopen("/home/Subdir1/D1/A2.txt", "w", 0666); // open the file A2.txt in write mode with 0666 permission
for(i=0;str2[i]!='\n';i++){
fputc(str2[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
char strB[] = "abcdefghij";
fptr = fopen("/home/Subdir1/d2/B1.ccc", "w", 0664); // open the file B1.ccc in write mode with 0664 permission
for(i=0;strB[i]!='\n';i++){
fputc(strB[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
char strB2[] = "you are beautiful";
fptr = fopen("/home/Subdir1/d2/B2.ccc", "w", 0664); // open the file B2.ccc in write mode with 0664 permission
for(i=0;strB2[i]!='\n';i++){
fputc(strB2[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
char strB3[] = "you are terrible!";
fptr = fopen("/home/Subdir1/d2/B3.ccc", "w", 0664); // open the file B3.ccc in write mode with 0664 permission
for(i=0;strB3[i]!='\n';i++){
fputc(strB3[i],fptr); // write the content to file using fputc
}
fclose(fptr); // close the file pointer
}
Create the following subdirectory structure under your home directory using Program 1.