Skip to content

Instantly share code, notes, and snippets.

@muhasturk muhasturk/reverse_file.c

Last active Mar 24, 2020
What would you like to do?
Reverse File
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
FILE *file;
file = fopen("./oz.txt", "r+");
if (file == NULL)
printf ("Error - Couldn't open file\n");
fseek(file, 0, SEEK_END); // move file pointer to end of file
long size = ftell(file); // file pointer position == character count in file
fseek(file, 0, SEEK_SET); // move back to beginning of file
char* buffer = (char*) malloc(size * sizeof(char));
fread(buffer, sizeof(char), size, file); // read file contents to buffer
for(long i = 0; i < size/2; ++i)
buffer[i] = buffer[size-i-1];
fseek(file, 0, SEEK_SET); // The fread set the file pointer to the end so we need to put it to the front again.
fwrite(buffer, sizeof(char), size, file); // Write reverted content
return 0;

This comment has been minimized.

Copy link

blackstar0223 commented Mar 24, 2020

It does not reverse all of the texts in your file, just half of them. Did u intend to do so?


This comment has been minimized.

Copy link
Owner Author

muhasturk commented Mar 24, 2020

Hey, this piece of 'c' code was written 5 years ago and I could remember what yo asked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.