Im writing a program that takes a files content and reverses it and I want to put the reversed output into an output.txt file but nothing is getting copied into the output.txt the content is reversing fine but not copying over to the other file. (I have created an "output.txt" file that I want the orignial text to be copied over to. here is my code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *fp, *fp2;
char ch;
int a;
char c;
int s;
int i, pos;
int choice = 0;
fp = fopen(argv[1],"r");
if (fp == NULL)
{
printf("file doesnt exist \n");
}
fp2 = fopen(argv[2], "w");
if(fp2 == NULL) {
printf("ERRORRR");
exit(1);
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
i = 0;
while (i < pos){
i++;
fseek(fp,-i,SEEK_END);
ch = fgetc(fp);
printf("%c", ch);
fputc(fp, fp2);
}
/*
do {
a = fgetc(fp);
fputc(a,fp2);
}
while ( a != EOF);
*/
return 0;
}