Does anyone Know How to..?

This is a discussion on Does anyone Know How to..? within the C Programming forums, part of the General Programming Boards category; Does anyone know how to write a function with ONLY ANSI/ISO C functions, that will read a character from stdin ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    Question Does anyone Know How to..?

    Does anyone know how to write a function with ONLY ANSI/ISO C functions, that will read a character from stdin without the user having to press the [ENTER] key? I have know idea, tried and failed.

  2. #2
    Unregistered
    Guest
    Actually... I think this is quite impossible using stdin.
    And impossible using standard functions.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its not entirely impossible. You could write something to get handle input yourself and process it accordingly. It may require a little asm though. It works and you can even do it in c with no asm. The only thing is that you will have to compile with a dos compiler since win32 doesn't allow you to handle such hardware I/O directly.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    No it is possible and used often I think. I will have to look it up, though. Nevermind here it is:

    the functions are:

    setvbuf(FILE *stream, char *buffer, int buf_type, size_t buf_size);

    AND

    setbuf(FILE *stream, char *buffer);



    Good Luck!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    Hold on, there's more:

    To use setbuf, set the second parameter to NULL.

    To use setvbuf, set the third parameter to _IONBF.

    Of course the FILE stream will be "stdin".
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    Unhappy

    I tried using setvbuf(); but I still can't get it not to echo. Im stumped. What is "asm"? Maybe someone could post a list with definitions or more info on setvbuf() and setbuf()?

  7. #7
    Unregistered
    Guest

    Talking

    kwigibo>>

    um, below are two deff's about the functions you were wondering about, both from

    "http://network-services.uoregon.edu/doc/cygnus/libc/libc_76.html#SEC76"

    it seems to be a very handy site.. anyways, best of luck =)

    ************************************************** *

    setvbuf---specify file or stream buffering
    Synopsis
    #include <stdio.h>
    int setvbuf(FILE *fp, char *buf,
    int mode, size_t size);


    Description
    Use setvbuf to specify what kind of buffering you want for the file or stream identified by fp, by using one of the following values (from stdio.h) as the mode argument:

    _IONBF
    Do not use a buffer: send output directly to the host system for the file or stream identified by fp.
    _IOFBF
    Use full output buffering: output will be passed on to the host system only when the buffer is full, or when an input operation intervenes.
    _IOLBF
    Use line buffering: pass on output to the host system at every newline, as well as when the buffer is full, or when an input operation intervenes.
    Use the size argument to specify how large a buffer you wish. You can supply the buffer itself, if you wish, by passing a pointer to a suitable area of memory as buf. Otherwise, you may pass NULL as the buf argument, and setvbuf will allocate the buffer.


    Warnings
    You may only use setvbuf before performing any file operation other than opening the file.

    If you supply a non-null buf, you must ensure that the associated storage continues to be available until you close the stream identified by fp.


    Returns
    A 0 result indicates success, EOF failure (invalid mode or size can cause failure).


    Portability
    Both ANSI C and the System V Interface Definition (Issue 2) require setvbuf. However, they differ on the meaning of a NULL buffer pointer: the SVID issue 2 specification says that a NULL buffer pointer requests unbuffered output. For maximum portability, avoid NULL buffer pointers.

    Both specifications describe the result on failure only as a nonzero value.

    Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write.

    *************************************************

    setbuf---specify full buffering for a file or stream
    Synopsis
    #include <stdio.h>
    void setbuf(FILE *fp, char *buf);


    Description
    setbuf specifies that output to the file or stream identified by fp should be fully buffered. All output for this file will go to a buffer (of size BUFSIZ, specified in `stdio.h'). Output will be passed on to the host system only when the buffer is full, or when an input operation intervenes.
    You may, if you wish, supply your own buffer by passing a pointer to it as the argument buf. It must have size BUFSIZ. You can also use NULL as the value of buf, to signal that the setbuf function is to allocate the buffer.


    Warnings
    You may only use setbuf before performing any file operation other than opening the file.

    If you supply a non-null buf, you must ensure that the associated storage continues to be available until you close the stream identified by fp.


    Returns
    setbuf does not return a result.


    Portability
    Both ANSI C and the System V Interface Definition (Issue 2) require setbuf. However, they differ on the meaning of a NULL buffer pointer: the SVID issue 2 specification says that a NULL buffer pointer requests unbuffered output. For maximum portability, avoid NULL buffer pointers.

    Supporting OS subroutines required: close, fstat, isatty, lseek, read, sbrk, write.






    --------------------------------------------------------------------------------




    -twans
    ..latas!
    (yeh yeh.. bout to register..)

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    just so you dont get confused.. i posted the last msg under "unregistered" however, the previous posts by: "unregistered" were not by me..

    =)

    -twanskies!

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    I am trying to use these functions too. If you use

    setbuf(stdin,NULL);

    Then where is your char that the user typed?
    It doen't seem wait for the char to be typed?
    I tryed this

    char c;
    setvbuf(stdin,&c,_IONBF,sizeof(char))

    but nothing ever got put into c, that I could tell.
    I put it in a while loop like this
    while(c!='q')
    {
    setvbuf(stdin,&c,_IONBF,sizeof(char));
    }
    It never exited the loop when I typed 'q'?
    Can someone that knows how these functions work post an example.
    Thanks

  10. #10
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    I already knew the syntax(I referred to a texbook), but I still can't get anything at all to work, I had similar problems to "mkeef".

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    5
    I posted the question elsewhere and they said to use
    #include <conio.h>

    void main()
    {
    char c;
    c = getch();
    }

    I have not tried it yet.....

  12. #12
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    problem is i don't have getch(), also if you read the top i need to use ANSI/ISO C standard functions portability.

    Also, this is not homework or an assignment it is for general interest and a learning experience.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is no portable ansi-c way to do this. sorry.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21