Files
inverT/functions.c

36 lines
1.2 KiB
C

#include <main.h>
int ask_file() {
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){ // 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
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
fclose(info.fd); // Close the file
}
else { // Otherwiser
perror(info.filename); // print failed to open the file
}
return 0;
}