How to use scanf to accept the default value by simply pressing Enter?

I was wondering if anyone could help me with this:

printf("Enter path for mount drive (/mnt/Projects) \n"); scanf("%s", &cMountDrivePath); 

Is it possible to allow the user to just press Enter and accept the default value (in this case: / mnt / Projects)? Currently, if the user presses Enter, the cursor simply moves to the next line, and input is still required.

I get the impression that scanf does not allow this, and in this case, what should I use?

Thanks!

+4
source share
6 answers

I think fgets() gives you a bit more input options, especially empty lines.

+4
source

No, scanf() cannot be configured to accept the default value. To make it even funnier, scanf() cannot accept an empty string as valid input; the conversion specifier% s tells scanf() ignore leading spaces, so it won’t return until you type something non-empty, then press Enter or Return.

To accept empty input, you need to use something like fgets() . Example:

 char *defaultPath = "/mnt/Projects"; ... printf("Enter path for mount drive (%s): ", defaultPath); fflush(stdout); /** * The following assumes that cMountDrivePath is an * array of char in the current scope (ie, declared as * char cMountDrivePath[SIZE], not char *cMountDrivePath) */ if (fgets(cMountDrivePath, sizeof cMountDrivePath, stdin) != NULL) { /** * Find the newline and, if present, zero it out */ char *newline = strchr(cMountDrivePath, '\n'); if (newline) *newline = 0; if (strlen(cMountDrivePath) == 0) // input was empty { strcpy(cMountDrivePath, defaultPath) } } 

EDIT

Changed default to defaultPath ; forgot that default is a reserved word. Bad code monkey, no banana!

+4
source

scanf is evil because it does not check boundaries!

Use getline from the GNU C library .

If your compiler supports C ++, just use istream :: getline

+1
source

Or, if you decide to use scanf, use a format string that limits the number of characters that will be scanned (to prevent buffer overflow errors).

For example, if your buffer is 128 bytes (including the nul terminator):

 scanf("%127s", &cMountDrivePath); 
+1
source

No, scanf () cannot be accepted for accepting "default values". You should get the input, check it for empty and set the default value yourself.

As you found out, scanf () is harder; I agree with extraneon that fgets () is probably what you are looking for.

Edit:

The reason why scanf () does not respond ... scanf () returns after it a) processed all conversion specifiers or b) found an error. As with all conversion qualifiers ("% s" in this case), missing spaces are skipped. In addition, "% s" reads all characters to the next space.

Hit return inserts '\ n' into standard input. So, if you hit return before you type anything else (except maybe a few tabs and spaces), scanf () will skip your input and continue to wait for input without spaces.

Whatever you enter, scanf () will not β€œsee” it until you hit return, because your input is stored in the console buffer. (Otherwise, you would not be able to go back through your input.) Pressing again returns "\ n" to the input stream, so your "% s" ends even if you did not enter any tabs / spaces on the line that you dialed.

If you enter spaces, your cMountDrivePath will only contain the first β€œword” before the first space, so leaving spaces in the directory or file names makes those damn annoying. It would also leave the rest of the line in the input buffer, so the next time you call scanf (), it won’t immediately ask for input, but proceed with processing what was buffered during the previous call β€” another annoying detail for scanf () newcomers to follow.

Hope this makes it a little easier for you (and other people yet to come).

0
source

On the man page for scanf :

RETURN VALUE
These functions return the number of successfully selected input elements and are assigned, which may be less than provided, or even zero in an early match event.

You can use this information to scan without user input (only for a new line \n ):

 printf("Enter path for mount drive (/mnt/Projects) \n"); if (scanf("%80[^\n]s", &cMountDrivePath) < 1) { /* assign default value */ ; } 
0
source

Source: https://habr.com/ru/post/1302029/


All Articles