Compare commits

9 Commits
1.0 ... genesis

Author SHA1 Message Date
Ricardo
e01d096b82 Added a TODO file to write down my future ideas of improvement 2022-05-30 19:55:50 +00:00
Ricardo
90a67543f0 Got rid of some useless functions and commented all the code 2022-05-30 19:52:15 +00:00
Ricardo
8a1ae2ca1a Miner fixing of main.h and main.c files 2022-05-28 06:41:25 +00:00
Ricardo
07054660f8 Minor fix in main.c file 2022-05-28 01:12:34 +00:00
Ricardo
0f21eb3b76 Pimping README.md file, details details everywhere 2022-05-28 01:10:56 +00:00
Ricardo
fe9fa5596c Pimping README.md file, still on it 2022-05-28 01:09:24 +00:00
Ricardo
71dd003bf9 Pimping README.md file, again 2022-05-28 01:06:05 +00:00
Ricardo
d124e873e8 Pimping README.md file 2022-05-28 01:04:46 +00:00
Ricardo
8cd98516be Minor fix on main.c 2022-05-28 00:40:51 +00:00
6 changed files with 72 additions and 64 deletions

View File

@@ -1,4 +1,16 @@
# inverT
Invert character by character given text file content
## Invert character by character given text file content
Usage: ./inverT -i 'some.text.file'<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-help &nbsp;&nbsp;&nbsp; Print help and exit.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-v &nbsp;&nbsp;&nbsp;&nbsp; Print application version and exit.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-i &nbsp;&nbsp;&nbsp;&nbsp; Invert text characters.<br>
Example: "./inverT -i test.txt\n"
Input(_from text inside the file_) :
> Murder for a jar of red rum
Output :
> mur der fo raj a rof redruM

4
TODO Normal file
View File

@@ -0,0 +1,4 @@
Implement something similar to rev from util-linux 2.34

View File

@@ -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);
ft = ftell(info.fd);
while(i < ft)
{
i++;
fseek(info.fd, -i, SEEK_END);
printf("%c", fgetc(info.fd));
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");
printf("\n"); // print newline to clear the terminal
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;

View File

@@ -1,20 +1,17 @@
#ifndef __CHCOUNT_H__
#define __CHCOUNT_H__
#ifndef __MAIN_H__
#define __MAIN_H__
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
//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
//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
@@ -23,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

BIN
inverT Normal file → Executable file

Binary file not shown.

55
main.c
View File

@@ -12,53 +12,56 @@
// 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: ./chcount -c [<some.text.file>]\n\
Usage: ./inverT -i [<some.text.file>]\n\
-help Print help and exit.\n\
-v Print application version and exit.\n\
-i Invert text caracters.\n\
Example: ./chcount -c test.txt\n",
-i Invert text characters.\n\
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 parameters!!!");
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;