FGETC()
#include <stdio.h>
void run_main_program() {
FILE *file = fopen("/home/chef/workspace/tail.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
}
else{
fseek(file, 0, SEEK_END); // Go to end of file using seek
long size = ftell(file); // Find size using tell
if (size - 1 >= 0) {
fseek(file, -1, SEEK_END) // Go to last character using seek
char ch = fgetc(file); // Read last character
printf("Last character: %c\n", ch);
}
if (size - 2 >= 0) {
fseek(file, -2, SEEK_END); // Go to second last character using seek
char ch = fgetc(file); // Read second last character
printf("Second last character: %c\n", ch);
}
if (size - 3 >= 0) {
fseek(file, -3, SEEK_END); // Go to third last character using seek
char ch = fgetc(file); // Read third last character
printf("Third last character: %c\n", ch);
}
// Close file
fclose(file);
}
}
int main() {
run_main_program();
return 0;
}
Last updated