Write a Java or C program (the choice is yours) for file/directory processing ac
ID: 3562281 • Letter: W
Question
Write a Java or C program (the choice is yours) for file/directory processing according to the following rules. The program requested for this project must have a text menu like this:
0
Explanation / Answer
#include #include #include #include #include #include void listcurrntdir(const char *name,int level) { DIR *dir; struct dirent *entry; if (!(dir = opendir(name))) return; if (!(entry = readdir(dir))) return; do { if (entry->d_type == DT_DIR) { char path[1024]; int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); path[len] = 0; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; printf("%*s[%s] ", level*2, "", entry->d_name); } else printf("%*s- %s ", level*2, "", entry->d_name); } while (entry = readdir(dir)); closedir(dir); } void listdir(const char *name, int level) { DIR *dir; struct dirent *entry; if (!(dir = opendir(name))) return; if (!(entry = readdir(dir))) return; do { if (entry->d_type == DT_DIR) { char path[1024]; int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); path[len] = 0; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; printf("%*s[%s] ", level*2, "", entry->d_name); listdir(path, level + 1); } else printf("%*s- %s ", level*2, "", entry->d_name); } while (entry = readdir(dir)); closedir(dir); } void del(const char *name) { int ret = remove(name); if(ret == 0) { printf("File deleted successfully"); } else { printf("Error: unable to delete the file or file unavailable"); } } void display(const char *file_name) { char ch; FILE *fp; fp = fopen(file_name,"r"); if( fp == NULL ) { perror("Error while opening the file. "); return; } printf("The contents of file are "); while( ( ch = fgetc(fp) ) != EOF ) { printf("%c",ch); } printf(" "); fclose(fp); } int Encrypt(char *dir_name) { char ch, file_name[25],newfilename[25],passwd[20]; FILE *fp,*fp1; printf("Enter the name of file you wish to encrypt: "); scanf("%s",file_name); printf("Enter the password: "); scanf("%s",passwd); printf("Enter the name of file you wish to save encrypted data: "); scanf("%s",newfilename); strcat(dir_name,file_name); strcat(dir_name,newfilename); fp = fopen(dir_name,"r"); // read mode fp1= fopen(dir_name,"w");//write mode if( fp == NULL ) { perror("Error while opening the file. "); return 0; } int len=strlen(passwd); int total=0; while( ( ch = fgetc(fp) ) != EOF ) { printf("%c",ch); char ch1=ch ^ passwd[total%len]; fprintf(fp1,"%c",ch1); total++; } fclose(fp); fclose(fp1); return 0; } int Decrypt(char *dir_name) { char ch, file_name[25],newfilename[25],passwd[10]; FILE *fp,*fp1; printf("Enter the name of file you wish to Decrypt: "); scanf("%s",file_name); printf("Enter the password: "); scanf("%s",passwd); printf("Enter the name of file you wish to save Decrypyted file: "); scanf("%s",newfilename); strcat(dir_name,file_name); strcat(dir_name,newfilename); fp = fopen(file_name,"r"); // read mode fp1= fopen(newfilename,"w");//write mode if( fp == NULL ) { perror("Error while opening the file. "); return 0; } int len=strlen(passwd); int total=0; while( ( ch = fgetc(fp) ) != EOF ) { printf("%c",ch); char ch1=ch ^ passwd[total%len]; fprintf(fp1,"%c",ch1); total++; } fclose(fp); fclose(fp1); return 0; } int main() { char dirname[100],filename[25],path[100]; int choice; while(1) { printf(" 0 – Exit 1 – Select directory 2 – List directory content (first level) 3 – List directory content (all levels) 4 – Delete file 5 – Display file 6 – Encrypt file (XOR with password) 7 – Decrypt file (XOR with password) "); scanf("%d",&choice); switch(choice) { case 0: exit(0); case 1: printf("Enter the directory [absolute] name(/home/user/)"); scanf("%s",dirname); break; case 2: listcurrntdir(dirname,0); break; case 3: listdir(dirname,0); break; case 4: printf("Enter File name you wish to delete: "); scanf("%s",filename); strcpy(path,dirname); strcat(path,filename); del(path); break; case 5: printf("Enter File name you wish to display: "); scanf("%s",filename); strcpy(path,dirname); strcat(path,filename); display(path); break; case 6: Encrypt(dirname); break; case 7: Decrypt(dirname); break; } } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.