Question: Some changes are requested in the C code below. This program runs from the terminal and it is necessary to start the program each time

Some changes are requested in the C code below. This program runs from the terminal and it is necessary to start the program each time (./main). But the request is this: After the user runs the program once from the terminal, just typing the command /h should be enough.

What is required from the display function is that the 'more' command in Linux fulfills its function. In other words, if the user writes 10 when he runs the display command, the lines in the file should be separated by 10 by 10.

The program should be ready to deal with faulty situations such as the file does not exist, the file name already exists, an invalid command, and in such cases, it should print the appropriate message on the screen.

#include

#include

#include

int main(int argc, char* argv[]) {

FILE *fp;

if(strcmp(argv[argc-1], "/h") == 0)

{

if(strcmp(argv[1], "create") == 0)

printf("To create a file do \"./main create filename.txt\" ");

else if(strcmp(argv[1], "remove") == 0)

printf("To remove a file do \"./main remove filename.txt\" ");

else if(strcmp(argv[1], "rename") == 0)

printf("To rename a file do \"./main rename oldname.txt newname.txt\" ");

else if(strcmp(argv[1], "copy") == 0)

printf("To copy a file do \"./main copy source.txt destination.txt\" ");

else if(strcmp(argv[1], "move") == 0)

printf("To move a file do \"./main move filename.txt ../Documents/filename.txt\" ");

else if(strcmp(argv[1], "append") == 0)

printf("To append a file do \"./main append filename.txt text\" ");

else if(strcmp(argv[1], "writeat") == 0)

printf("To write at a specific location to file do \"./main writeat filename.txt location text\" ");

else if(strcmp(argv[1], "clear") == 0)

printf("To clear a file do \"./main clear filename.txt\" ");

else if(strcmp(argv[1], "display") == 0)

printf("To display a file do \"./main display filename.txt\" ");

else

printf("The syntax of the command is incorrect");

return 0;

}

if(strcmp(argv[1], "create") == 0) {

fp = fopen(argv[2], "w");

}

else if(strcmp(argv[1], "remove") == 0) {

remove(argv[2]);

}

else if(strcmp(argv[1], "rename") == 0) {

rename(argv[2], argv[3]);

}

else if(strcmp(argv[1], "copy") == 0) {

char ch;

FILE *dest;

fp = fopen(argv[2], "r");

dest = fopen(argv[3], "w");

if (fp == NULL) {

printf("No such file or directory");

exit(-1);

}

while ((ch = fgetc(fp)) != EOF)

fputc(ch, dest);

}

else if(strcmp(argv[1], "move") == 0) {

char ch;

FILE *dest;

fp = fopen(argv[2], "r");

dest = fopen(argv[3], "w");

if (fp == NULL) {

printf("No such file or directory");

exit(-1);

}

else if (dest == NULL) {

printf("No such file or directory");

exit(-1);

}

while ((ch = fgetc(fp)) != EOF)

fputc(ch, dest);

fclose(fp);

fp = fopen(argv[2], "w");

remove(argv[2]);

}

else if(strcmp(argv[1], "append") == 0) {

char *ap;

ap = argv[3];

FILE *dest;

dest = fopen(argv[2], "a");

if (dest == NULL) {

printf("No such file or directory");

exit(-1);

}

for(int i = 0; i < strlen(ap); i++)

{

fputc(argv[3][i], dest);

}

}

else if(strcmp(argv[1], "writeat") == 0) {

char *ap;

ap = argv[4];

FILE *dest;

FILE *fout;

dest = fopen(argv[2], "r+b");

if (dest == NULL) {

printf("No such file or directory");

exit(-1);

}

fseek(dest, strtol(argv[3], NULL, 10), SEEK_SET);

for(int i = 0; i < strlen(ap); i++)

{

fputc(argv[4][i], dest);

}

}

else if(strcmp(argv[1], "clear") == 0){

fp = fopen(argv[2], "w");

if (fp == NULL) {

printf("No such file or directory");

exit(-1);

}

}

else if(strcmp(argv[1], "display") == 0) {

char ch;

fp = fopen(argv[2], "r");

if (fp == NULL) {

printf("No such file or directory");

exit(-1);

}

do

{

/* Read single character from file */

ch = fgetc(fp);

/* Print character read on console */

putchar(ch);

} while(ch != EOF); /* Repeat this if last read character is not EOF */

/* Done with this file, close file to release resource */

fclose(fp);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!