diff --git a/functions.c b/functions.c index 4be6d72..8898c5f 100644 --- a/functions.c +++ b/functions.c @@ -2,41 +2,33 @@ int ask_file() { - printf ("Insert name of the file to be read : "); - fgets(info.filename, info.fileNameSize, stdin); - info.filename[strlen(info.filename)-1] = '\0'; - invert_file(); - return 0; -} - -int get_file(char *fileName) { - - strcpy(info.filename, fileName); - invert_file(); - return 0; + printf ("Insert name of the file to be read : "); // print this + fgets(info.filename, info.fileNameSize, stdin); // get the file from user input + info.filename[strlen(info.filename)-1] = '\0'; // turn the last character into zero + return 0; // to clear the newline character in there } int invert_file(void) { - if ((info.fd= fopen(info.filename, "r")) != NULL){ - int ft, i = 0; + if ((info.fd= fopen(info.filename, "r")) != NULL){ // If the file opens + int ft, i = 0; // make this two variables to help + // going thru the text + fseek(info.fd, 0, SEEK_END); // take the file descriptor pointer + // to the end of the file + ft = ftell(info.fd); // Count the file total number of characters - fseek(info.fd, 0, SEEK_END); + while(i < ft) { // While the characters dont finish + i++; // increment one character + fseek(info.fd, -i, SEEK_END); // go back one character in the text + printf("%c", fgetc(info.fd)); // print current character + } + printf("\n"); // print newline to clear the terminal - ft = ftell(info.fd); - while(i < ft) - { - i++; - fseek(info.fd, -i, SEEK_END); - printf("%c", fgetc(info.fd)); - } - printf("\n"); - - fclose(info.fd); + fclose(info.fd); // Close the file } - else { - perror(info.filename); + else { // Otherwiser + perror(info.filename); // print failed to open the file } return 0; diff --git a/include/main.h b/include/main.h index a5eab64..5fadbec 100644 --- a/include/main.h +++ b/include/main.h @@ -6,13 +6,12 @@ //Function declarations -int ask_file(); // It has no flags, it just prompts for filename -int get_file(char *fileName); // Gets the filename passed on has an argument -int invert_file(void); // Invert the file stored in the variable info.filename +int ask_file(); // It has no flags, it just prompts for filename +int invert_file(void); // Invert the file stored in the variable info.filename -//This are general parameters for the program -struct parameters { + +struct parameters { //This are general parameters for the program char filename[100]; // Filename where the text is FILE *fd; // File descriptor for the text file @@ -21,6 +20,6 @@ struct parameters { char help[]; // Help menu to be printed on screen }; -struct parameters info; +struct parameters info; // Declare struct info of type parameters #endif diff --git a/inverT b/inverT old mode 100644 new mode 100755 index 1a38537..5f21d97 Binary files a/inverT and b/inverT differ diff --git a/main.c b/main.c index 07ef0b7..23a3fad 100644 --- a/main.c +++ b/main.c @@ -12,11 +12,11 @@ // Necessary to initalize my struct with all the info struct parameters info = { - .filename = "EMPTY", - .fd = NULL, - .version = "1.0", - .fileNameSize = 80, -// Below is the variable that olds the text to dislpay for help + .filename = "EMPTY", // Here we store the file name + .fd = NULL, // File descriptor + .version = "1.0", // Software version + .fileNameSize = 80, // How many characters has the filename, including extention +// Below is the variable that holds the text to dislpay for help .help = "\n\ ------------------------------------------------\n\ Usage: ./inverT -i []\n\ @@ -30,35 +30,38 @@ Example: ./inverT -i test.txt\n", int main (int argc, char *argv[]) { - if (argc == 1) { - ask_file(); + if (argc == 1) { // If only one argument + ask_file(); // Ask user for the file name and location + invert_file(); // and invert the file content } - else if (argc == 2) { - if (strncmp(argv[1], "-help", 5) == 0) { - printf("%s\n", info.help); + else if (argc == 2) { // If two arguments + if (strncmp(argv[1], "-help", 5) == 0) { // Check if the flag is '-help' + printf("%s\n", info.help); // and print help info } - else if (strncmp(argv[1], "-v", 2) == 0) { - printf("Version %s\n", info.version); + else if (strncmp(argv[1], "-v", 2) == 0) { // Check if the flag is '-v' + printf("Version %s\n", info.version); // and print version info } - else { - get_file(argv[1]); + else { // Otherwise + strcpy(info.filename, argv[1]); // save the second argument into info.filename + invert_file(); // and invert the file content } } - else if (argc == 3) { - if(strncmp(argv[1], "-i", 2) == 0) { - get_file(argv[2]); + else if (argc == 3) { // If three arguments + if(strncmp(argv[1], "-i", 2) == 0) { // Check if the flag is '-i' + strcpy(info.filename, argv[2]); // save the third argument into info.filename + invert_file(); // and invert the file content } - else { - printf("\nInvalid flag!!!"); - printf("%s\n", info.help); + else { // Otherwise + printf("\nInvalid flag!!!"); // print this + printf("%s\n", info.help); // and then print help info } } - else { - printf("\nTo many arguments!!!"); - printf("%s\n", info.help); + else { // Otherwise if more then three arguments + printf("\nTo many arguments!!!"); // print this + printf("%s\n", info.help); // and then print help info } return 0;