Write a c program to display the contents of the file in reverse order Also copy the contents from one file to another file. Your input and output must look something
like this Input to the reverse function
input :My Captain
output: niatpaC yM
Answers
Explanation:
Initialise previous length of the text as 0.
Find the length of the current line and add it to the previous length. This given the next starting index of the new line.
Repeat the above steps till the end of the file.
Initialise the array of length of the given message in the given file.
Now rewind your file pointer and place the last pointer of the text to arr[K – 1] where K is the length of the array using fseek().
Print the length of the last line and decrease K by 1 for printing the next last line of the file.
Repeat the above steps untill K is equals to 0.
Below is the implementation of the above approach:
// C program for the above approach
#include <stdio.h>
#include <string.h>
#define MAX 100
// Function to reverse the file content
void reverseContent(char* x)
{
// Opening the path entered by user
FILE* fp = fopen(x, "a+");
// If file is not found then return
if (fp == NULL) {
printf("Unable to open file\n");
return;
}
// To store the content
char buf[100];
int a[MAX], s = 0, c = 0, l;
// Explicitly inserting a newline
// at the end, so that o/p doesn't
// get effected.
fprintf(fp, " \n");
rewind(fp);
// Adding current length so far +
// previous length of a line in
// array such that we have starting
// indices of upcoming lines
while (!feof(fp)) {
fgets(buf, sizeof(buf), fp);
l = strlen(buf);
a = s += l;
}
// Move the pointer back to 0th index
rewind(fp);
c -= 1;
// Print the contents
while (c >= 0) {
hope this will help you
Explanation:
Program to Reverse the contents of a ...
- *C Program to Reverse the contents of a file and print it.
- # include <erron h>
- long count_characters (File*);
- Void msin (int argc, char* argv▯)
- int I;
- Long cnt;
- Char ch,ch1
- File* fp1,*fp2;
More but I am answering this much only.