My code *somewhat* works but it still doesn't do what it's supposed to do.
I'm trying to systematically read contents of a binary file into a 1KiB buffer, modify the buffer, and write the contents of the buffer back to the same file, continue until:
filesize <= position - buffersize
when that happens it means I'm at the very end of a file, so read the last part of file which should be:
filesize - position
long. So modify the buffersize, read in buffersize worth of data, modify it, and write it back.
That's the theory, and now you'll get to see my crappy code. I'm sure there are lots of reasons why it doesn't work, but I came here to learn, so go ahead and tell me if you see a mistake.
Code:
void modifyfile(unsigned char *file) {
char *BUFFER[1024]; // this value doesn't really make it 1024 bytes big.
// Array starts at 0, so it should be BUFFER[1023] to make it 1KiB
// wrong???
long BUFFERSIZE = (sizeof(BUFFER) / sizeof(BUFFER[0]));
long position;
long filesize;
unsigned int i;
FILE *file_ptr;
// open file
unsigned long int num;
if ((file_ptr = fopen(file, "rb+")) == NULL) {
printf("Error opening file: %s \n", file);
exit(1);
}
// check the filesize
if ((fseek(file_ptr, 0, SEEK_END)) != 0) {
printf("Error in seek operation on file %s : errno \n", file);
exit(1);
}
// set filesize variable
filesize = ftell(file_ptr);
// go back
rewind(file_ptr);
// initial position
position = ftell(file_ptr);
// we're not at the last part of file
while (position < (filesize - sizeof(BUFFER))) {
// read BUFFERSIZE worth of data(should be 1KiB, but isn't)
fread(BUFFER, sizeof(BUFFER[0]), BUFFERSIZE, file_ptr);
// move the position in file BACK
if ((fseek (file_ptr, position, SEEK_SET)) != 0) {
printf("Error in seek operation on file %s : errno \n", file);
exit(1);
}
// modify data, this *should*(but doesn't) let us
// modify each byte in buffer one by one
for(i = 0 ; i < BUFFERSIZE ; i++) {
BUFFER[i] = 0x00; // an example - fill with zeros - 0x41 should fill with ASCII '!'
}
// write the modified buffer back to file
fwrite(BUFFER, sizeof(BUFFER[0]), BUFFERSIZE, file_ptr);
// now remember the advanced position
// I guess I could also use something like:
// position += BUFFERSIZE;
position = ftell(file_ptr);
}
// we're now at the last part of file
if (position > (filesize - sizeof(BUFFER))) {
// recalculate buffersize
BUFFERSIZE = ((filesize - position)/sizeof(BUFFER[0]));
// read BUFFERSIZE worth of data(now it should be 1KiB or smaller)
fread(BUFFER, sizeof(BUFFER[0]), BUFFERSIZE, file_ptr);
// move the position in file BACK
if ((fseek (file_ptr, position, SEEK_SET)) != 0) {
printf("Error in seek operation on file %s : errno \n", file);
exit(1);
}
// same as before
for(i = 0 ; i < BUFFERSIZE ; i++) {
BUFFER[i] = 0x00; // an example - fill with zeros
}
// write the modified buffer back to file
fwrite(BUFFER, sizeof(BUFFER[0]), BUFFERSIZE, file_ptr);
}
fclose(file_ptr);
printf("processed file: %s\n", file);
printf("file size: %db\n", filesize);
printf("BUFFER size: %d\n", BUFFERSIZE);
}