I am writing a program to copy a text file "input.txt" to "output.txt" however instead of copying the first line to the last line I would need to inversely copy the last line to the first line to the "output.txt" file. Could someone please give some advice thanks!
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char filename[]={"input.txt"};
char filename2[]={"output.txt"};
char a;
FILE *inptr, *outptr;
inptr = fopen(filename, "r");
if(inptr == NULL)
{
printf("The file could not be opened!\n");
system("pause");
return 1;
}
outptr = fopen(filename2, "w");
if(outptr == NULL)
{
printf("The file could not be opened!\n");
printf("Creating a new file......\n");
system("pause");
return 1;
}
while((fscanf(inptr, "%c", &a)) != EOF)
{
fprintf(outptr, "%c", a);
puts("A character was copied!\n\n");
}
fclose(inptr);
fclose(outptr);
system("pause");
return 0;
}
For example lets say there are 3 lines in the text file:
Hi Bye Hello
so I would need to copy the contexts to another file but it starts from:
Hello Bye Hi
Thanks!