0
我有一個二進制文件,在下面的格式,其中每個字是4個字節長:C瞭解4個字節是一次
add arg1 arg2 arg3 // where in this example it would store the value of arg1+arg2 in arg3
遇到麻煩然而找出一種方法來讀取文件在前4個字節是操作碼的地方,接下來的8到16個字節代表每行下3個字。下面是我目前還沒有工作的代碼。
#define buflen 9000
char buf1[buflen];
int main(int argc, char** argv){
int fd;
int retval;
if ((fd = open(argv[1], O_RDONLY)) < 0) {
exit(-1);
}
fseek(fd, 0, SEEK_END);
int fileSize = ftell(fd);
for (int i = 0; i < fileSize; i += 4){
//first 4 bytes = opcode value
//next 4 bytes = arg1
//next 4 bytes = arg2
//next 4 bytes = arg3
retval = read(fd, &buf1, 4);
}
}
我不知道如何一次得到4個字節,然後評估它們。任何人都可以提供一些幫助嗎?
4字節= 1個字。並且每行最多有4個字,但是某些操作碼(如ex打印)可能只使用8個字節而不是全部16個 – Valrok
1)如果您必須讀取4個字節,爲什麼數組是'9000' ? 2)在for循環中,每次迭代都覆蓋'buf'。 –
哪個字節順序是存儲的數字?例如。 '1'可以存儲爲'00 00 00 01'或者'01 00 00 00'(或者別的什麼) –