Skip to content Skip to sidebar Skip to footer

How to Read a File Backwards Using Read in C++

Introduction

In this mail service, I am going to write a plan to print the contents of a file in reverse order in c programming. What does this mean?

Nosotros volition read a file from the end character past character. For writing this program, we need to perform the following steps.

Too Read: Switch Instance in C Plan to Summate Surface area of Circumvolve and Triangle

  1. Open a file in reading mode.
  2. Motion the file pointer to the end of the file using fseek() function.
  3. Count full number of bytes of the file using ftell() part.
  4. Read the file from the end character by character.

Now, before going further, let's encounter the expected output kickoff. This is the c program of file handling. Therefore, we demand one file that nosotros are going to open in the reading mode. Suppose that file is abc.txt and its contents are

Input File: abc.txt

abcd efgh ijkl mnop qrst uvwxyz

Also Read: C Plan to Print Multiples of five using exercise-while loop

Now, nosotros need to contrary the contents using our c programme.

Expected Output

print contents of file in reverse order in c
Fig. Output – print contents of file in reverse order in c

Equally you tin see from the above expected output. These are the contents of the input file abc.txt in the reverse order.

Now, let usa see the actual c program to opposite the contents of a file.

Print Contents of File in Reverse Order in C

#include<stdio.h> #include<stdlib.h> int main() {     FILE *fp;     int ft,i=0;     fp=fopen("abc.txt","r");     if(fp==Nil)     {         printf("ERROR");         return 0;     }     fseek(fp,0,SEEK_END);     ft=ftell(fp);     while(i<ft)     {         i++;         fseek(fp,-i,SEEK_END);         printf("%c",fgetc(fp));     }     fclose(fp);     return 0; }        

Explanation

At present, I am going to explain the of import part of this programme.

ane. Open a file in read way.

Nosotros have to brandish the contents of a file. Then there is no need to write anything in the file. Therefore, we are going to open up this file in the read style using the statement

fp = fopen(" abc.txt ", "r");        

Here, r means the read way.

Likewise Read: C Program to Remove Zeros from a number

2. Move the file pointer to the end of the file using fseek() function.

For reading a file in the contrary order, nosotros need to reach the end of the file. Nosotros can do this by using the fseek part. The fseek() function moves to a specific location. Meet the following code.

fseek(fp, 0, SEEK_END );

Here, fp means the file pointer which is associated with the input file abc.txt, 0 ways don't move anywhere, SEEK_END ways move this file arrow to the end of the file.

iii. Count total number of bytes of the file using ftell() function

Nosotros demand to count the total number of bytes because we are going to use a while loop. Using this while loop, we volition movement the file arrow character by graphic symbol. For this, we need one status which terminates the loop. Run across the following code.

ft=ftell(fp); while(i<ft)

In the second step, we have moved the file pointer to the end of the file. But, nosotros don't know how many characters or bytes are there from commencement to end. The office ftell() will count the total bytes from offset to the end and that value will be stored in the variable ft.

As well Read: The while loop in C Programming

In the while loop, nosotros volition check whether the value of i is less than ft or not. The initial value of i is 0. When the value of i becomes greater than ft and then, that loop will be terminated.

4. Read the file from the end character by character.

In the while loop, I have written the following code.

i++; fseek(fp,-i,SEEK_END); printf("%c",fgetc(fp));

The while loop will echo the higher up three lines. In the first line, we are incrementing the value of i.

In the 2d line, the fseek() function will move the file pointer by the 'i' graphic symbol. The negative sign tells us that the file pointer will movement in reverse order.

The third line will impress that character which is pointed out by the value of i. For reading the character from the file, we are using the fgetc() part which reads the single character from the file.

I hope yous have understood this programme. If yous know the Hindi language, so I have already made a video for this program. You can watch it here.

Cheers.

Some Of import C Programs

  1. Program in C to Observe Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Assortment in C using switch case
  4. C Program to Add Alternate Elements of a 2d Array
  5. Arrays in C for Complete Beginners
  6. C Plan to Find Surface area of a Circle using Preprocessor
  7. Programme in C to Remove White Spaces and Comments from a File
  8. C Programme to Impress Numbers Except Multiples of n
  9. Contrary a Number using getchar and putchar function in c
  10. The while loop in C Programming

millerthatten.blogspot.com

Source: https://hplusacademy.com/print-contents-of-file-in-reverse-order-in-c/

Post a Comment for "How to Read a File Backwards Using Read in C++"