C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement
  • Algorithm
  • Explanation

Was this helpful?

  1. File Input and Output

Program: Printing the Contents of a File in Reverse Order

Problem Statement

Write a C program that reads the contents of a file and prints them in reverse order. The program should open the file, read its contents, and then print the characters or lines in reverse order.

Algorithm

  1. Open the File for Reading

    • Use the fopen function to open the file in read mode.

    FILE *filePointer;
    filePointer = fopen("input.txt", "r");
    • Check if the file is successfully opened.

    if (filePointer == NULL) {
        // Handle file opening error
    }
  2. Find the Size of the File

    • Use fseek and ftell to find the size of the file.

    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 beginning
  3. Read and Print the Contents in Reverse

    • Use a loop to read characters or lines in reverse order and print them.

    char character;
    for (long i = fileSize - 1; i >= 0; i--) {
        fseek(filePointer, i, SEEK_SET);
        character = fgetc(filePointer);
        printf("%c", character);
    }
    • Alternatively, if the file contains lines, read and print them in reverse order.

    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--;
    }
  4. Close the File

    • Close the file after reading and printing.

    fclose(filePointer);

Explanation

This program reads the contents of a file and prints them in reverse order. The size of the file is determined, and then characters or lines are read in reverse order using fseek and fgetc. Finally, the file is closed.

Ensure that the input file exists and has the necessary permissions for reading. This program provides a simple way to print the contents of a file in reverse order.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousProgram: Converting Characters in a File to UppercaseNextThe Standard C Library

Was this helpful?