| file-backward.c - Et program der læser en fil baglæns. | Lektion 10 - slide 13 : 28 Program 1 |
#include <stdio.h>
#define MAXSTRING 100
int main(void){
char filename[MAXSTRING];
int c;
FILE *ifp;
fprintf(stderr, "\nInput a filename: ");
scanf("%s", filename);
ifp = fopen(filename, "r");
fseek(ifp, 0, SEEK_END);
fseek(ifp, -1, SEEK_CUR);
while (ftell(ifp) > 0) {
c = getc(ifp);
putchar(c);
fseek(ifp, -2, SEEK_CUR);
}
/* The only character that has not been printed
is the very first character in the file. */
fseek(ifp, 0, SEEK_SET);
c = getc(ifp);
putchar(c);
return 0;
}