0

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;
}
Chris
  • 474
  • 2
  • 8
  • 22
  • 1
    1. Remove the C++ tag. 2. This sounds like you should be using a debugger to track down your problem – UnholySheep Dec 04 '16 at 17:48
  • 1
    Why even count the characters? Just read with 'fgetc()' until EOF. – Zach P Dec 04 '16 at 17:50
  • 1
    The C++ tag might be appropriate here. Since Luma is obviously compiling this using Microsoft's tools (`_CRT_SECURE_NO_WARNINGS`), he might actually be using a C++ compiler because Microsoft's C compiler is 20 years out of date. If so, he's just writing C++ code using C idioms, and the C++ standard is the one that would need to be followed. – Cody Gray - on strike Dec 04 '16 at 17:52
  • 1
    @CodyGray In that case the C tag is inappropriate. – melpomene Dec 04 '16 at 17:54
  • 1
    When trying this code, I see it fully working for a full file. – Florian Heer Dec 04 '16 at 17:55
  • Hmm, I can't actually compile this as C++ because my compiler doesn't like `void main`. – melpomene Dec 04 '16 at 17:55
  • 1
    `void main` is illegal in both C and C++. The `main` function always returns `int`. – Cody Gray - on strike Dec 04 '16 at 18:00
  • Proper indentation helps readability. – e0k Dec 04 '16 at 18:03
  • 1
    See [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) for a more nuanced version of "`main()` should return an `int` and not `void`". There is a platform-specific variation on `main()` that is allowed by the Microsoft C compiler. – Jonathan Leffler Dec 04 '16 at 18:08
  • 3
    Please read the man pages for the functions you use - and attend to compiler warnings. `gets_s()` takes a `size_t sizeInCharacters` argument which you fail to supply. Edit: that is a compiler error, not a warning. So this is not your actual code since there is no executable. – Weather Vane Dec 04 '16 at 18:09
  • 2
    `fseek(InputFile1, -2L, 1);` is UB "For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file and whence shall be SEEK_SET." – chux - Reinstate Monica Dec 04 '16 at 18:25
  • 1
    What compiler are you using? (Several comments raise compilation issues.) – e0k Dec 04 '16 at 18:26

0 Answers0