Program: Printing the Contents of a File in Reverse Order
Problem Statement
Algorithm
FILE *filePointer; filePointer = fopen("input.txt", "r");if (filePointer == NULL) { // Handle file opening error }fseek(filePointer, 0, SEEK_END); // Move to the end of the file long fileSize = ftell(filePointer); // Get the size of the file fseek(filePointer, 0, SEEK_SET); // Move back to the beginningchar character; for (long i = fileSize - 1; i >= 0; i--) { fseek(filePointer, i, SEEK_SET); character = fgetc(filePointer); printf("%c", character); }char line[100]; // Adjust the size based on the maximum line length fseek(filePointer, 0, SEEK_END); long currentPosition = ftell(filePointer); while (currentPosition > 0) { fseek(filePointer, currentPosition - 1, SEEK_SET); character = fgetc(filePointer); if (character == '\n') { // Print the line printf("%s\n", line); // Reset the line buffer memset(line, 0, sizeof(line)); } else { // Concatenate the character to the line buffer strncat(line, &character, 1); } currentPosition--; }fclose(filePointer);