用于反转文件但仅输出最后一行的程序

时间:2016-12-04 17:47:21

标签: c file

我一直在尝试在C中编写一个程序,其中读入一个文件并输出到另一个文件但所有字符都反转,但这只会将输入文件的最后一行放入输出文件中,并且不是整个文件。我哪里错了? :)

#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;
}

0 个答案:

没有答案