#include #include "dbg.h" #define MAX_FILENAME 2048 int main(int argc, char *argv[]) { int rc = -1; char *filename; int nchars = 0; int nlines = 0; int i; long seekpos; char c; FILE *fp; check(argc > 1, "You must provide a filename as arg 1"); filename = argv[1]; fp = fopen(filename, "r"); check(fp != NULL, "Failed to open \"%s\".", filename); while((c = fgetc(fp)) != EOF) { nchars++; if (c == '\n') { nlines++; if (nlines % 10 == 0) { printf("At line %d of \"%s\"\n", nlines, filename); } } } printf("\"%s\" contains %d chars\n", filename, nchars); printf("Current position of \"%s\" is %ld\n", filename, ftell(fp)); printf("Writing out \"%s\" in reverse:\n", filename); for (i = 0; i < nchars; i++) { seekpos = (long)nchars - (i + 1); rc = fseek(fp, seekpos, SEEK_SET); check(rc != -1, "Failed to seek to %ld", seekpos); c = fgetc(fp); check(c != EOF, "Failed to read char at %ld", seekpos); putc(c, stdout); } rewind(fp); printf("\n\nRewound \"%s\" to position %ld\n", filename, ftell(fp)); rc = fclose(fp); check(rc != -1, "Failed to close \"%s\".", filename); return 1; error: return 0; }