diff --git a/CommandConsole.exe b/CommandConsole.exe index ff3a81d..2cc5da5 100644 Binary files a/CommandConsole.exe and b/CommandConsole.exe differ diff --git a/MainCommands.h b/MainCommands.h index 5e76a2c..98dd774 100644 --- a/MainCommands.h +++ b/MainCommands.h @@ -4,7 +4,14 @@ #include #include #include +#include +#include +#include + +#define Max 8192 +#define COPYMODE 0644 #define SIZE 260 +#define BUFFER_SIZE 4096 char change_dir[100]; @@ -234,6 +241,7 @@ void help() printf(">>rfile - This will rename the file with the name you want.\n\n"); printf(">>rdr - This will rename the folder with the name you want.\n\n"); printf(">>getf - This will confirm you that the fille or folder name you have given is present in the directory that you specified.\n\n"); + printf(">>findf - This will find the specified word in a given file and print the line where it is located.\n\n"); } //function to list all the files in the current directory. @@ -494,7 +502,8 @@ void pcwd() } // Function to rename folder -void renameFolder(){ +void renameFolder() +{ getchar(); char firstname[SIZE], lastname[SIZE]; printf("Enter the current name of the folder: "); @@ -505,16 +514,19 @@ void renameFolder(){ lastname[strlen(lastname) - 1] = 0; int value = rename(firstname, lastname); - if(!value){ + if (!value) + { printf("Successfully changed\n"); } - else{ + else + { printf("Cannot change\n"); } } // Function to find a file or folder in a folder -void getf(){ +void getf() +{ getchar(); char foldername[SIZE]; printf("Enter the name of the folder or directory first: "); @@ -527,7 +539,8 @@ void getf(){ { printf("can't find %s\n", foldername); } - else{ + else + { printf("Enter the filename or foldername to find: "); char reciepent[SIZE]; fgets(reciepent, SIZE, stdin); @@ -539,4 +552,56 @@ void getf(){ } } closedir(dp); +} + +// Function to find the location of a word in a file +void findf() +{ + getchar(); + char word[SIZE]; + FILE *fptr; + + int i = 0, line = 1; + int confirm; + int linearray[1000]; + + char filename[SIZE]; + printf("Enter filename: "); + fgets(filename, SIZE, stdin); + filename[strlen(filename) - 1] = 0; + + char str[BUFFER_SIZE]; + + fptr = fopen(filename, "r"); + if (fptr == NULL) + { + printf("Cannot open file.\n"); + } + else + { + printf("Enter the word to find: "); + fgets(word, SIZE, stdin); + word[strlen(word) - 1] = 0; + printf("The word is find in line number:- \n"); + while (fgets(str, BUFFER_SIZE, fptr) != NULL) + { + if (strstr(str, word)) + { + linearray[i] = line; + confirm = 1; + printf("%d\n", linearray[i]); + } + else + { + confirm = 0; + } + i++; + line++; + } + if (confirm == 0) + { + printf("Actually! eh word is not present.\n"); + } + } + fclose(fptr); } \ No newline at end of file diff --git a/README.md b/README.md index 8612bdf..9e98a1b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The main program is written in the main.c file and the functions written there a - Rename the file: rfile - Rename the folder: rdr - To confirm that a file or folder is present in a directory specified by you: getf +- To get the number of line of a file in which a specified word is present: findf All the functions are written in **MainCommands.h** diff --git a/main.c b/main.c index 8b63658..fcf5a52 100644 --- a/main.c +++ b/main.c @@ -129,6 +129,9 @@ void main_loop() { getf(); } + else if(strcmp(cmd_str, "findf") == 0){ + findf(); + } else { printf("Error: Wrong Command\n\tRun command which is there. Type help for information\n");