简体   繁体   English

C - 使用 fscanf 从文件中读取十六进制和字符

[英]C - Using fscanf for reading hexadecimal and characters from files

I have two files.我有两个文件。 The first file (in the pointer key_file ) has the following content in hexadecimal (notice that every two characters corresponds to a hexadecimal byte):第一个文件(在指针key_file )具有以下十六进制内容(注意每两个字符对应一个十六进制字节):

18FC5E66F9F205488BFC0E9E

And the other file (in the pointer p_text_file ) has the following content另一个文件(在指针p_text_file )具有以下内容

hernandariov

I want to write the bitwise XOR between these two values and save the result in another file (in the pointer c_text_file ).我想在这两个值之间写入按位异或并将结果保存在另一个文件中(在指针c_text_file )。 I write the following (abbreviated) code in C:我在 C 中编写了以下(缩写)代码:

char p_text_char;
unsigned char k_char;
for(int i = 0; i < LEN; i++) {
    fscanf(key_file, "%2hhX", &k_char);
    fscanf(p_text_file, "%c", &p_text_char); 
    fprintf(c_text_file, "%02X", k_char ^ p_text_char);
}

But when I execute the code, it exits with a non-zero value, ie, there is an error.但是当我执行代码时,它以非零值退出,即出现错误。 After try many solutions, I found that if I change the type of p_text_char to int , the code works pretty well, but I want to know why.在尝试了很多解决方案后,我发现如果将p_text_char的类型p_text_charint ,代码运行得很好,但我想知道为什么。 In fact, I want to keep p_text_char as char and k_char as unsigned char because these are the natural types and sizes for this variables in the problem I am working on (the one-time-pad encryption scheme).事实上,我想将p_text_char保留为char并将k_charunsigned char因为这些是我正在处理的问题(一次性加密方案)中此变量的自然类型和大小。 I am working on Windows with MinGw compiler.我正在使用 MinGw 编译器在 Windows 上工作。

Thank you.谢谢你。

- EDIT - - 编辑 -

After run a debugger in this code, I found that there is a "segmentation fault".在这段代码中运行调试器后,我发现存在“分段错误”。 I try to minimize the code in order to find the error.我尝试最小化代码以找到错误。 First, I try the following code with the files as showed before:首先,我对之前显示的文件尝试以下代码:

#include <stdio.h>

#define LEN 12

int main() {

    FILE *key_file;

    key_file = fopen("key.txt", "r");

    if(key_file == NULL) {
        printf("ERROR: cannot read the files.");
        return 1;
    }

    unsigned char k_char;
    for(int i = 0; i < LEN; i++) {
        fscanf(key_file, "%2hhX", &k_char);
    }

    fclose(key_file);
    return 0;
}

After one iteration of the for loop, the VS Code editor shows me a "segmentation fault".在 for 循环的一次迭代之后,VS Code 编辑器向我显示了“分段错误”。 And when I change k_char to int , everything works fine.当我将k_char更改为int ,一切正常。 I will keep posting my findings about this problem.我将继续发布我对这个问题的发现。

The version of VS C that OP is using may not understand "%hhX" . OP 使用的 VS C 版本可能无法理解"%hhX" Formally this is undefined behavior and *scanf() may ignore "hh" .正式这是未定义的行为*scanf()可能会忽略"hh" Thus code "works" with an int .因此代码与int一起“工作”。

To cope with C89-like compilers that lack "hh" understanding, use an intermediate object.要处理缺乏"hh"理解的类 C89 编译器,请使用中间对象。

// fscanf(key_file, "%2hhX", &k_char);
unsigned utemp = 0;
fscanf(key_file, "%2X", &utemp);
k_char = utemp;

... or use a compiler that conforms at least to the 21 year old C99 standard, if not C11 or C17. ...或者使用至少符合 21 年历史的 C99 标准的编译器,如果不是 C11 或 C17。 C2x due out in a year or two. C2x 在一两年内到期。

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

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