Threads merged.
yea thanks, i coudlnt remember about C, i should of justOriginally Posted by dwks
paid attention to what going on instead of trying to remember
back, i was thinking c++.
for example in c++:
Code:char letter; ifstream in("file.txt", ios::binary); in.read( (char*)&letter, sizeof(letter) );
When I attempt to read from the end of the file, it apparantly fails the initial read. I've tried playing around with the code, but this is the closest I can get.Code:#include <stdio.h> //prototypes int start_binary (void); int main (void) { start_binary () return (0); } int start_binary (void) { int loop = 0; char x[10] = "ABCDEFGHIJ" char X = ' '; FILE *fp = fopen ("prime_binary.txt", "wb"); if (!fp) { printf ("\nERROR! Unable to open!\n\n"); return (0); } //read file from end to beginning and print to screen for (loop = 0; loop < 10; loop++) { fseek (fp, -1 * loop * sizeof (x[0]), SEEK_END); fread (&X, sizeof (x[0]), 1, fp); printf ("%c", x); } printf ("\n\n"); return (0); } /* My output: JIHGFEDCB */
How do I read the file properly from end to beginning using fseek/fread in any kind of loop?
mw
Of course it does. Basic math is what's failing you:When I attempt to read from the end of the file, it apparantly fails the initial read.Anything times zero is zero.Code:for (loop = 0; loop < 10; loop++) { fseek (fp, -1 * loop * sizeof (x[0]), SEEK_END);
Quzah.
Hope is the first step on the road to disappointment.
You could try reading the entire file into a block of sorts then just calling it from end to beginning...
Example..
That should read a file from end to beginning and output each line to the screen... looking back over it it's a little messy in spots(sorry I'm in a hurry) but you should get the general idea of a way to do it.Code:#include <stdio.h> #include <string.h> #define MAX_LINES 10000 #define MAXSIZE 2048 char buffer[MAXSIZE]; long posn_block[MAX_LINES]; long last_line; FILE *infp; void skip_and_read(long); void countlines(FILE *); main(int argc, char** argv) { int i; int cur_line; if( (infp = fopen(argv[1], "rb")) == NULL) printf("Could not open file %s\n", argv[1]); countlines(infp); cur_line = last_line; for(i=0; i<last_line; i++) { skip_and_read(cur_line); printf("%s", buffer); cur_line--; } fclose(infp); return 0; } void skip_and_read(long line) { fseek(infp, posn_block[line], SEEK_SET); memset(buffer, '\0', MAXSIZE); fgets(buffer, MAXSIZE, infp); } void countlines(FILE *fp) { int done = FALSE; last_line = 0; while (! done) { posn_block[last_line] = ftell(fp); if (fgets(buffer, MAXSIZE, fp) == NULL) done = TRUE; else { last_line++; if(last_line >= MAX_LINES) { last_line = MAX_LINES; done = TRUE; } } } last_line--; }
BTW just so no one jumps down my throat I broke it up into functions and used global variables because I figured it would be easier to understand.
Last edited by Sysop_fb; 08-25-2005 at 02:02 PM.
This fixed it... I had thought the zero calculation would mean "start at the very last position", and sure enough it did. However, the fread function apparantly reads immediately after the pointer's position. Makes sense...Code://read file from end to beginning and print to screen for (loop = 0; loop < 10; loop++) { fseek (fp, -1 * (loop + 1) * sizeof (x[0]), SEEK_END); fread (&X, sizeof (x[0]), 1, fp); printf ("%c", x); }
Once I added 1 to my calculations it repositioned the pointer where I needed it.
mw