6.0k views
3 votes
a. For a file whose size (in terms of number of bytes) is less than or equal to 12, then duplicate the contents of the file so that its total size would be twice as many as its original size.

1 Answer

1 vote

Answer:

#include <stdio.h>

int main()

{

int check;

char* dirname = "Subdir1";

check = mkdir(dirname,0766);

// check if directory is created or not

if (!check)

{

printf("Subdir1 Directory created\\");

char *dirname1="/Subdir1/D1";

if(mkdir("/Subdir1/D1", 0777)==-1)

{

printf("D1 Directory created\\");

int A1 = open ("/Subdir1/D1/A1.txt", 0666);

write(A1, "abcdefghij\\", strlen("abcdefghij\\"));

close(A1);

int A2 = open ("/Subdir1/D1/A2.txt", 0666);

write(A2, "you are beautiful\\", strlen("you are beautiful\\"));

close(A2);

int A3 = open ("/Subdir1/D1/A3.txt", 0666);

write(A3, "this is terrible!\\", strlen("this is terrible!\\"));

close(A3);

}

else

{

printf("Unable to create directory D1\\");

exit(1);

}

if(mkdir("/Subdir1/D2", 0777)==-1)

{

printf("D2 Directory created\\");

int B1 = open ("/Subdir1/D2/B1.ccc", 0664);

write(B1, "abcdefghij\\", strlen("abcdefghij\\"));

close(B1);

int B2 = open ("/Subdir1/D2/B2.ccc", 0664);

write(B2, "you are beautiful\\", strlen("you are beautiful\\"));

close(B2);

int B3 = open ("/Subdir1/D2/B3.ccc", 0664);

write(B3, "this is terrible!\\", strlen("this is terrible!\\"));

close(B3);

}

else

{

printf("Unable to create directory D2\\");

exit(1);

}

if(mkdir("/Subdir1/D3", 0777)==-1)

{

printf("D3 Directory created\\");

int C1 = open ("/Subdir1/D3/C1.txt", 0644);

write(C1, "abcdefghij\\", strlen("abcdefghij\\"));

close(C1);

int C2 = open ("/Subdir1/D3/C2.ddd", 0644);

write(C2, "you are beautiful\\", strlen("you are beautiful\\"));

close(C2);

int C3 = open ("/Subdir1/D3/C3.txt", 0644);

write(C3, "this is terrible!\\", strlen("this is terrible!\\"));

close(C3);

}

else

{

printf("Unable to create directory D2\\");

exit(1);

}

}

else {

printf("Unable to create directory Subdir1\\");

exit(1);

}

return 0;

}

Program 2:

#include <stdio.h>

long int findSize(char file_name[])

{

// opening the file in read mode

FILE* fp = fopen(file_name, "r");

// checking if the file exist or not

if (fp == NULL) {

printf("File Not Found!\\");

return -1;

}

fseek(fp, 0L, SEEK_END);

// calculating the size of the file

long int res = ftell(fp);

// closing the file

fclose(fp);

return res;

}

// Driver code

int main()

{

FILE *fptr1;

char c;

char file_name[] = {"D1.txt"};

long int res = findSize(file_name);

if (res != -1)

{

printf("Size of the file is %ld bytes \\", res);

if(res<=12)

{

fptr1 = fopen("D1.txt", "r");

c = fgetc(fptr1);

while (c != EOF)

{

fputc(c, fptr1);

c = fgetc(fptr1);

printf(c);

}

}

}

return 0;

}

User Upalr
by
5.2k points