I have been trying to write a program in C where a file is read in and output to another file but with ALL of the characters reversed, however this only puts the last line of the input file into the output file reversed, and not the whole file. Where am I going wrong? :)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <errno.h>
long count_characters(FILE *);
void main() { // the main program
long count;
char character;
FILE *InputFile1, *OutputFile;
char fileName1[50], fileName2[50];
printf("Enter the name of the input file: ");
gets_s(fileName1);
printf("Enter the name of the output file: ");
gets_s(fileName2);
InputFile1 = fopen(fileName1, "r");
OutputFile = fopen(fileName2, "w");
count = count_characters(InputFile1);
fseek(InputFile1, -1L, 2);
while (count)
{
character = fgetc(InputFile1);
fputc(character, OutputFile);
fseek(InputFile1, -2L, 1);
count--;
}
fclose(InputFile1);
fclose(OutputFile);
}
long count_characters(FILE *f)
{
fseek(f, -1L, 2);
long lastPosition = ftell(f);
lastPosition++;
return lastPosition;
}