Reputation: 433
I was wondering if someone could please help me with this:
printf("Enter path for mount drive (/mnt/Projects) \n");
scanf("%s", &cMountDrivePath);
Is it possible to allow the user to simply press Enter and accept the default (in this case: /mnt/Projects)? At present, if the user presses Enter, the cursor simply goes to the next line and input is still required.
I get the impression scanf does not allow this, in which case, what should I use?
Thanks!
Upvotes: 4
Views: 8828
Reputation: 23
maybe you can use this:
void defaultScanf (char *value, char defaultValue)
{
char trash;
*value = getchar();
/* set your default valut */
if (*value == '\n') {
*value = defaultValue;
} else {
*value -= '0'; /* transform char to int */
/* clean your buffer */
do trash = getchar();
while (trash != '\n' && trash != EOF);
}
}
Upvotes: 0
Reputation: 6999
From the man page for scanf
:
RETURN VALUE
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
You can use this information to scan for no user input (just a newline \n
):
printf("Enter path for mount drive (/mnt/Projects) \n");
if (scanf("%80[^\n]s", &cMountDrivePath) < 1) {
/* assign default value */ ;
}
Upvotes: 0
Reputation: 123458
No, scanf()
cannot be configured to accept a default value. To make things even more fun, scanf()
cannot accept an empty string as valid input; the "%s" conversion specifier tells scanf()
to ignore leading whitespace, so it won't return until you type something that isn't whitespace and then hit Enter or Return.
To accept empty input, you'll have 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 (i.e., 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!
Upvotes: 6
Reputation: 70263
No, scanf() can not be made to accept "default values". You have to get the input, check it for emptyness, and assign a default value yourself.
As you found out, scanf() is tricky; I concur with extraneon that fgets() is probably what you are looking for.
Edit:
The reason why scanf() is not reacting... scanf() returns after it a) processed all conversion specifiers or b) encountered an error. As with all conversion specifiers ("%s" in this case), leading whitespace is skipped. Additionally, "%s" reads all characters until the next whitespace.
Pressing return inserts a '\n' into standard input. So, if you press return before you entered anything else (except perhaps a couple of tabs and spaces), scanf() skips your input and proceeds to wait for non-whitespace input.
Whatever you enter, scanf() won't "see" it until you press return, because your input is held in the console buffer. (You wouldn't be able to backspace through your input otherwise.) Pressing return again enters "\n" into the input stream, so your "%s" is terminated even if you did not enter any tabs / spaces on the line you typed.
If you did enter whitespaces, your cMountDrivePath would only contain the first "word" up to the first whitespace, which is why leaving spaces in directory or file names makes things so damn annoying. It would also leave the rest of the line in the input buffer, so the next time you call scanf() it wouldn't immediately ask for input, but proceed to process whatever was buffered during the previous call - another annoying detail for scanf() beginners to watch out for.
I hope this cleared things up a bit for you (and others yet to come).
Upvotes: 0
Reputation: 27900
Or, if you're determined to use scanf, use a format string that limits the number of characters that will be scanned (to prevent buffer overrun errors).
For example, if your buffer is 128 bytes (including the nul terminator):
scanf("%127s", &cMountDrivePath);
Upvotes: 1
Reputation: 23950
I think fgets()
gives you a bit more possibilities in input, especially blank lines.
Upvotes: 4
Reputation: 10128
scanf is evil because it does no bounds-checking!
Please use getline from the GNU C library.
If your compiler supports C++, simply use istream::getline
Upvotes: 1