flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > msvcrt.dll, getting one symbol from console hint

Author
Thread Post new topic Reply to topic
twgt



Joined: 23 Jan 2007
Posts: 8
twgt
Some days ago, I tried to use function scanf from msvcrt.dll library for taking one char from console.
Every second attempt call function return value without asking user.
Code:
 cinvoke scanf,"%c",userinput    

I print 'y'
Code:
 cinvoke scanf,"%c",userinput1    

After this manipulation userinput contain 'y', but userinput1 contain 0x0A

in C language it can be viewed by next code
Code:
  unsigned int x, m;      
     scanf("%c",&x );     //x="y"
     scanf("%c",&m);     //m=0x0A
     scanf("%c",&m);     //m="y"
    


for correct using of this method of taking input char use "%s" as argument for scanf-function.

PS. functions getchar() and getc() also not returning char when they called twice
PPS. Thanks Stiver!
Post 03 Jan 2008, 04:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
twgt: I am not sure what you are saying. Is that a question or are you offering advice?
Post 03 Jan 2008, 06:36
View user's profile Send private message Visit poster's website Reply with quote
twgt



Joined: 23 Jan 2007
Posts: 8
twgt
revolution, it's experience. Smile

Look at real sample.

Code:
.loop:                   ;wait for connect
        ..............................................................
        cinvoke printf,<"Accept connection?[y/n]: ">
        mov dword[userinput],0
        cinvoke scanf,"%c",userinput
        cinvoke printf,<13,10>
        cmp [userinput],'y'
        jz @accept_connection
        cinvoke printf,<"Connection not accepted",13,10,"Wait for another connection",13,10>
        invoke closesocket,[clientsock]
        ;mov [clientsock],0
        jmp .loop 
    


Log of work:
Quote:

Accept connection?[y/n]: n

Connection not accepted
Wait for another connection

Accept connection?[y/n]:

Connection not accepted
Wait for another connection

Accept connection?[y/n]: n

Connection not accepted
Wait for another connection

Accept connection?[y/n]:

Connection not accepted
Wait for another connection

Understand?
Post 03 Jan 2008, 07:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
You might want to think about draining the keyboard buffer before asking the user for input. But it depends on how your program operates as to whether that is a good alternative.
Post 03 Jan 2008, 07:44
View user's profile Send private message Visit poster's website Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 245
Location: US
Yardman
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 03:08; edited 1 time in total
Post 03 Jan 2008, 08:01
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
twgt: "scanf("%c", ...)" is NOT waiting for user to type one character. It is reading one character from STDIN. Stdin can be redirected from file, in that case user doesn't type anything.

If stdin is not redirected from file, it reads from console. Any reading from console causes program to stop, and lets user type one line, regardless of how much you are reading from it. After you type one line, desired amount of characters are returned, and rest stay buffered, and are returned in sebsequent reads.

For example, in your demo, try to type "abcdyynnyn" and then hit tner.
Post 03 Jan 2008, 09:46
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.