简体   繁体   中英

How do I exit my program by pressing ENTER using scanf_s?

So i've done some googling and found that using the code:

#include <stdio.h>

void main()
{
    char ch;

    ch = fgetc(stdin);
    if (ch == '\n')
    {
        printf("\n");
    }
    else
    {
        printf("\n");
    }
}

does exactly what I want to however pasting this code lines in my other project that has scanf_s seems to not prompt for the user input at 'ch = fgetc(stdin)'. I was wondering if there is a way i can change ch = fgetc(stdin) to a scanf code such that it can read ENTER and exit the program.

Most scanf specifiers will leave a newline pending in the input stream.
Thus immediately following a scanf , fgetc will consume a newline.

The best I can come up with is to use the scanset %1[\n] to scan a single newline. Scan for the newline afterwards to consume the pending newline and scan before the desired input to get the leading newline that indicates input is complete and exit the loop.

The second loop accomplishes the same effect using fgets .

#include <stdio.h>
#include <stdlib.h>

#define LINE 99
//stringify to use in format string
#define SFS(x) #x
#define FS(x) SFS(x)

int main ( void) {
    char text[LINE + 1] = "";
    char newline[2] = "";
    int result = 0;

    while ( 1) {
        printf ( "enter text for scanf\n");
        scanf ( "%*[ \t\r]"); // scan and discard spaces, tabs, carriage return
        if ( 1 == ( result = scanf ( "%1[\n]", newline))) {
            printf ( "\tscanned leading newline\n");
            break;
        }
        if ( 1 == ( result = scanf ( "%"FS(LINE)"[^\n]", text))) {
            //as needed parse with sscanf, strtol, strtod, strcspn, strspn, strchr, strstr...
            printf ( "\tscanned text: %s\n", text);
            scanf ( "%*[ \t\r]"); // scan and discard spaces, tabs, carriage return
            if ( 1 == scanf ( "%1[\n]", newline)) {
                printf ( "\tscanned trailing newline\n");
            }
        }
        if ( EOF == result) {
            fprintf ( stderr, "EOF\n");
            return 1;
        }
    }

    while ( 1) {
        printf ( "enter text for fgets\n");
        if ( fgets ( text, sizeof text, stdin)) {
            if ( '\n' == text[0]) {
                printf ( "----newline only\n");
                break;
            }
            printf ( "----text is:%s\n", text);
            //as needed parse with sscanf, strtol, strtod, strcspn, strspn, strchr, strstr...
        }
        else {
            fprintf ( stderr, "EOF\n");
            return 2;
        }
    }

    return 0;
}

scanf doesn't read the trailing \n for strings but you can read a character with %c :

#include <stdio.h>

int main() {
        char c ; 
        scanf("%c", &c);
        if(c == '\n') {
           // ...
        }
        return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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