I get garbage from the file I'm reading when I try searching in reverse. I want to go in reverse until I find the '-' character and get all characters until the next '-' but I get garbage in the resulting character string... My intention is to start searching in the middle of the file.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define BLOCK 1024 void poll_file(FILE * file, char * message){ int pos = 0, i = 0, c = 0; fseek(file, 0, SEEK_END); pos = ftell(file) / 2; fseek(file, pos, SEEK_SET); while((c = fgetc(file)) != '-') fseek(file, --pos, SEEK_SET); while((c = fgetc(file)) != '-') message[i++] = (char)c; message[i] = '\0'; } int main(int argc, char ** argv){ char message[BLOCK]; FILE * file; if((file = fopen(argv[1], "r"))){ poll_file(file, message); printf("Message found in image: %s\n", message); } else printf("Error opening file\n"); return 0; }



LinkBack URL
About LinkBacks


