Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e01d096b82 | ||
|
90a67543f0 | ||
|
8a1ae2ca1a | ||
|
07054660f8 | ||
|
0f21eb3b76 | ||
|
fe9fa5596c | ||
|
71dd003bf9 | ||
|
d124e873e8 | ||
|
8cd98516be |
14
README.md
14
README.md
@@ -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>
|
||||
-help Print help and exit.<br>
|
||||
-v Print application version and exit.<br>
|
||||
-i 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
4
TODO
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
|
||||
|
||||
Implement something similar to rev from util-linux 2.34
|
46
functions.c
46
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;
|
||||
|
@@ -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
|
||||
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
|
||||
@@ -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
|
||||
|
55
main.c
55
main.c
@@ -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;
|
||||
|
Reference in New Issue
Block a user