c - Read bytes (chars) from buffer -
i'm working on steganography program in java. got advice able resolve task better in c program. try it, i'm pretty bad in c programing. read 1 gif
file , find byte used image separator (0x2c
gif format).
i tried write program:
int main(int argc, char *argv[]) { file *fileptr; char *buffer; long filelen = 0; fileptr = fopen("d:/test.gif", "rb"); // open file in binary mode fseek(fileptr, 0, seek_end); // jump end of file filelen = ftell(fileptr); // current byte offset in file rewind(fileptr); // jump beginning of file buffer = (char *)malloc((filelen+1)*sizeof(char)); // enough memory file + \0 fread(buffer, filelen, 1, fileptr); // read in entire file fclose(fileptr); // close file int = 0; for(i = 0; buffer[ ]; i++) { if(buffer[i] == 0x2c) { printf("next image"); } } return 0; }
could give me advice how repair loop?
could give me advice how repair loop?
option 1: don't depend on terminating null character.
for(i = 0; < filelen; i++) { if(buffer[i] == 0x2c) { printf("next image"); } }
option 2: add terminating null character before relying on it. potentially unreliable since reading binary file have embedded null characters in it.
buffer[filelen] = '\0'; for(i = 0; buffer[ ]; i++) { if(buffer[i] == 0x2c) { printf("next image"); } }
Comments
Post a Comment