繁体   English   中英

如何在C中使用scanf扫描整数值后跟字符

[英]How would I scan an integer value followed by a character using scanf in C

因此,我尝试从文本文件中扫描91h。 到目前为止,我已经尝试使用getchar和scanf,但是它们都没有将整个91h扫描为声明的字符串。 我在这里想念一个招吗?

这有效:

#include <stdio.h>

int main()
{
    FILE *file = fopen("test.txt", "r");

    int x1 = 0;
    int x2 = 0;
    int x3 = 0;
    int x4 = 0;
    fscanf(file, "%xh %xh %xh %xh", &x1, &x2, &x3, &x4);

    printf("x1: %x, x2: %x, x3: %x, x4: %x\n", x1, x2, x3, x4);
}

文件test.txt中的测试数据:

91h 82h 93h 94h
91h 82h 93h 94h

如果您只是将它们读入缓冲区:

char buff[128];
(f)scanf(file, "%s", buff);

如果将它们读入2个单独的变量中:

int num;
char var;
(f)scanf("%2d%c", &num, &var);

暂无
暂无

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

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