-1

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;
}
David C. Rankin
  • 75,900
  • 6
  • 54
  • 79
Alopax
  • 1
  • 1

2 Answers2

1

I think you have to open the second file in "Write mode".

fp2 = fopen(argv[2], "w");

And like @Sami Kuhmonen said there's something wrong with the line fputc(fp, fp2); As fputc takes two arguments.

fputc(int character, File *stream);
C Damo
  • 313
  • 2
  • 11
0

try this:

while (c){
     c=getc(fp1);
     fputc(c,fp2);
}
Adam
  • 23
  • 4