alternative to the fscanf()/vscanf() functions family for using an alternative validation key or how to create a custom language layout?

北城以北 提交于 2019-12-25 01:48:18

问题


I am rather knew in C. And the only way I know for setting a variable from stdin is by using thefscanf()/vscanf()functions family.

DOS operating systems are seeing a resurgence in the embedded market. The device which I am using is programmatically compatible with DOS and use a NEC V30Mx which feature the 80286 instruction set.
The problem is the built-in keyboard doesn’t feature the enter key and instead use the EXE key which enter the 0x1C00 key code (Alt enter).

So when I write a program like this :

#include <stdio.h>
#include <dir.h>
#include <dos.h>
int main(void) {
    int res, number = 0;
    printf("int value?");
    res = scanf("%d", &number);
    printf("The value is %d", number);
    return;
}

scanf()is called and "int value?" is displayed on the screen. I can write numbers and upercase letters. but I can’t validate the output since nothing feature the 0x1C0D key code, and the EXE key use a different keycode.
Of course C has no knowledge of keyboard but the C standard library binaries look for the 0x1C0D key code for entering a\n.

So is there an alternative to thefscanf()/vscanf()functions family which is part of the C standard library?

Update :

I thought most C standard libraries under dos use software implementation for doing this. I saw a DOS call for reading files and of course, using the number 0 cause to read from stdin.
So I assembled this example, loaded it on the calculator and find myself surprised the manufacturer preferred handling the issue in it’s built-in software rather than fixing the OS... :-( (I got the the same previous result)

Update

I didn’t thought about it but thanks to the comments, I forgot keyboard languages do key code remapping. So, creating a custom mapping for the qwerty keyboard would be an another way to solve the problem.


回答1:


The problem is scanf does not read past the bytes present in stdin that represent the EXE key.

Can you investigate to see what actual characters are read by getchar() when you press the EXE key, and write a function hasexe() that skips these if present. Call this function before or after the scanf().

Also check scanf() return value and check for EOF.




回答2:


The only way to solve such a problem with standard C library is to use getchar() (or fgetc() on stdin) to get a character one at a time, detect the EXE key yourself, NUL terminate the string and pass it to sscanf().



来源:https://stackoverflow.com/questions/29213463/alternative-to-the-fscanf-vscanf-functions-family-for-using-an-alternative-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!