简体   繁体   English

用 C 对文件进行二进制读取和写入

[英]Binary Reading from and Writing to a File in C

I need some help writing a program in C to reading and writing binary values from and to a file.我需要一些帮助,用 C 编写程序以从文件读取和向文件写入二进制值。 For this program I do not need the entire contents of the file (2048 bytes) read in, only the first 30 halfwords.对于这个程序,我不需要读入文件的全部内容(2048 字节),只需要读入前 30 个半字。

I do know how to write a program to do this for text files, but am new to binary.我确实知道如何编写程序来为文本文件执行此操作,但我是二进制文件的新手。 Do I need to allocate memory like for text files?我需要像文本文件一样分配内存吗? Would using a buffer be helpful?使用缓冲区会有帮助吗? Are the commands similar or different to fprintf, sscanf, and other syntax things?这些命令与 fprintf、sscanf 和其他语法内容相似还是不同?

this example should work这个例子应该有效

#define FILE "file path"
#define LEN 30
int main()
{
    int read;
    char *buffer;
    HANDLE hFile = CreateFile(FILE, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
                              FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile == INVALID_HANDLE_VALUE){
        printf("[-] CreateFile() failed.\n");
        return 0;}
    buffer = (char*)malloc(LEN);
    ReadFile(hFile, buffer, LEN, &read, NULL);
}

you read the file with no special flags.您读取没有特殊标志的文件。 you mentioned you only need the first 30 halfwords which are obviously the first 30 bytes.你提到你只需要前 30 个半字,这显然是前 30 个字节。 ReadFile() has the nNumberOfBytesToRead parameter, so all you need to pass 30. If you want you could try to read an .exe file and print what you got. ReadFile()nNumberOfBytesToRead参数,所以你只需要传递 30。如果你愿意,你可以尝试读取 .exe 文件并打印你得到的内容。 it should start with MZ and "this program cannot be run in dos mode" (you'd need to read more than 30 bytes in order to see all of it).它应该以 MZ 开头,并且“该程序不能在 dos 模式下运行”(您需要读取 30 多个字节才能看到所有内容)。 Some NULL bytes wouldn't let you simply print it so you should try that way一些 NULL 字节不会让您简单地打印它,所以您应该尝试这种方式

int PrintBuffer(char *buffer)
{
    int written;
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    WriteFile(out, buffer, LEN, &written, NULL);
    CloseHandle(out);
}

edit : a version with no winapi编辑:没有winapi的版本

#define PATH "file path"
#define LEN 30
#define HWORD 1
int main()
{
    int i = 0;
    char *buffer = (char*)malloc(LEN);
    FILE *f = fopen(PATH, "rb");

    if(!f) printf("[-] fopen() failed.\n");
    else printf("[+] fopen() succeeded.\n");

    if(fread(buffer, HWORD, LEN, f) != LEN) printf("[-] fread() failed.\n");
    else printf("[+] fread() succeeded.\n");

and now printing the data without the NULL bytes cutting the output too early现在打印没有 NULL 字节的数据过早地切断输出

    for(i = 0; i < LEN; i++) printf("%c", buffer[i]);
}

fread() reads "elements" from a file has the following parameters: fread()从文件中读取“元素”具有以下参数:
1. the buffer which will receive the read data. 1. 接收读取数据的缓冲区。
2. the size of each element in bytes. 2. 每个元素的大小(以字节为单位)。 Half word is a byte, so the size is 1.半字是一个字节,所以大小为 1。
3. the number of elements you want to read. 3.要阅读的元素数量。 in this case 30. 4. the FILE* you used for fopen() .在这种情况下 30. 4. 您用于fopen()的 FILE* 。

the return value is the amount of elements successfully read, it should be 30.返回值是成功读取的元素数量,应该是30个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM