我有一些問題與我的代碼,我不知道明白。我讀了一個文件(你可以嘗試使用任何文件)來獲取十六進制值。我試圖找到某些十六進制值並對其進行更改 - 這很有用,但它比應該更晚。例如:改變字符的鄰居
0xAA 0xAB 0xAC 0xAD 0XAE ... 0XCD 0xCE
我想更改0xAB,但我的代碼更改0XCD。不知道爲什麼會發生這種情況,但也許我錯了。還有一種方法可以自動獲取文件長度?我只是把一個緩衝區,是該文件的一部分,但我想獲得真正的長度。
#include <stdio.h>
#include <string.h>
#define FLEN 512
int convert_to_hex(char c);
int main(int argc, char *argv[]) {
char c;
int i = 0;
FILE *fp = fopen(argv[1],"rb");
for(i = 0; i < FLEN; i++) {
c = convert_to_hex(fgetc(fp));
printf("%02x ", c);
}
printf("\n");
}
int convert_to_hex(char c)
{
char hexVal[3];
sprintf(hexVal, "%02X", 0x69);
if(strncmp(&c, hexVal, 2) == 1) {
printf(">> %s ", hexVal); // indicate where it change (late)
return c + 1;
}
return c;
}
應該是字符hexVal [4],而不是hexVal [3],一個空終止字符 – novice
http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size -in-c - 在C++中獲取文件長度 – novice
@novice - 是的,它仍然具有相同的行爲。感謝其他鏈接 –