C 一次读取 4 个字节
C Read 4 bytes at a time
我有一个二进制文件,格式如下,每个字长 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 个字节然后对其求值。谁能帮帮我?
这将检查命令行是否包含文件名,然后尝试打开文件。
while 循环将一次读取文件 16 个字节,直到文件结束。 16 个字节分配给操作码和参数以根据需要进行处理。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( int argc, char *argv[])
{
int fd;
int retval;
int each = 0;
unsigned char buf[16] = {0};
unsigned char opcode[4] = {0};
unsigned char arg1[4] = {0};
unsigned char arg2[4] = {0};
unsigned char arg3[4] = {0};
if ( argc < 2) {//was filename part of command
printf ( "run as\n\tprogram filename\n");
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
printf ( "could not open file\n");
return 2;
}
while ( ( retval = read ( fd, &buf, 16)) > 0) {//read until end of file
if ( retval == 16) {//read four words
for ( each = 0; each < 4; each++) {
opcode[each] = buf[each];
arg1[each] = buf[each + 4];
arg2[each] = buf[each + 8];
arg3[each] = buf[each + 12];
}
//do something with opcode and arg...
}
}
close ( fd);
return 0;
}
我有一个二进制文件,格式如下,每个字长 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 个字节然后对其求值。谁能帮帮我?
这将检查命令行是否包含文件名,然后尝试打开文件。
while 循环将一次读取文件 16 个字节,直到文件结束。 16 个字节分配给操作码和参数以根据需要进行处理。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( int argc, char *argv[])
{
int fd;
int retval;
int each = 0;
unsigned char buf[16] = {0};
unsigned char opcode[4] = {0};
unsigned char arg1[4] = {0};
unsigned char arg2[4] = {0};
unsigned char arg3[4] = {0};
if ( argc < 2) {//was filename part of command
printf ( "run as\n\tprogram filename\n");
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
printf ( "could not open file\n");
return 2;
}
while ( ( retval = read ( fd, &buf, 16)) > 0) {//read until end of file
if ( retval == 16) {//read four words
for ( each = 0; each < 4; each++) {
opcode[each] = buf[each];
arg1[each] = buf[each + 4];
arg2[each] = buf[each + 8];
arg3[each] = buf[each + 12];
}
//do something with opcode and arg...
}
}
close ( fd);
return 0;
}