using scanf() to input strings with spaces
MacBook, Mac OS X (10.5.8)
MacBook, Mac OS X (10.5.8)
Tron55555 wrote:
I figured out that the gets() function will achieve the above functionality, but I would still like to know out of curiosity...
s Matches a sequence of non-white-space characters; the next pointer
must be a pointer to char, and the array must be large enough to
accept all the sequence and the terminating NUL character. The input
string stops at white space or at the maximum field width, whichever
occurs first.
If an l qualifier is present, the next pointer must be a pointer to
wchar_t, into which the input will be placed after conversion by
mbrtowc(3).
if there is a way to get scanf() to do this.
[ Matches a nonempty sequence of characters from the specified set of
accepted characters;
Also, when I use gets(), I get a message (on the command-line) right before the program gets to the gets() statement that says: "this program uses gets, which is unsafe"
SECURITY CONSIDERATIONS
The gets() function cannot be used securely. Because of its lack of bounds
checking, and the inability for the calling program to reliably determine
the length of the next incoming line, the use of this function enables mali-
cious users to arbitrarily change a running program's functionality through
a buffer overflow attack. It is strongly suggested that the fgets() func-
tion be used in all cases. (See the FSA.)
BUGS
Since it is usually impossible to ensure that the next input line is less
than some arbitrary length, and because overflowing the input buffer is
almost invariably a security violation, programs should NEVER use gets().
The gets() function exists purely to conform to ANSI X3.159-1989 (``ANSI
C'').
or something like that. Is there yet another function that I could use that will not produce this warning, or a way to get this warning not to appear?
Thank you.
char str1[1024];
char str2[128];
scanf("%s %s", str1, str2);
strcat(str1, " ");
strcat(str1, str2);
scanf(""%[^"]"", str);
fgets(userName, 32, stdin);
etresoft wrote:
The C++ equivalents are a joke
I ended up ... using the fgets() function, like so:
fgets(userName, 32, stdin);
fgets(userName, sizeof(userName), stdin);
I still have a few questions though:
1.) What would be the difference between this and using fgetln() instead? Is it simply that fgetln() reads until a newline character whereas fgets() reads until the given amount of characters or a newline character, whichever comes first? Is there any other difference worth noting here?
2.) Should I use fgets() for all my input from the command-line, or is it better to use scanf() when I know I'm only going to need to input one word?
size_t len;
char *input_line;
char userName[32];
input_line = fgetln(stdin, &len);
input_line[len-1] = ' '; # replaces newline with nul
sscanf(input_line,"%31s",userName);
Tron55555 wrote:
1.) What would be the difference between this and using fgetln() instead?
2.) Should I use fgets() for all my input from the command-line, or is it better to use scanf() when I know I'm only going to need to input one word?
etresoft wrote:
The C++ equivalents are a joke
By this I assume you are referring to cin and cout. I was interested to hear that. I would assume someone along the line thought they were superior otherwise they could have just stuck to the C I/O functions, right?
printf("The value of PI is %10.4f", M_PI);
std::cout << "The value of PI is";
std::cout << std::setw(10) << std::setprecision(5) << M_PI;
std::cout << std::endl;
BobHarris wrote:
Actually I'm a huge fan of sizeof().
Keith Barkley wrote:
fgetln() is not a part of C89. I don't know if it is C99, but it might not be included in all implementations.
etresoft wrote:
cin and cout are instances of C++ streams. It is the convoluted and difficult process of formatting streams in C++ that I am talking about. Whereas in C one can do something like:
cout << "There are " << numTreeApples << " apples in the tree and " << numGroundApples << " on the ground";
double chipsPurchased;
printf(" Enter how many dollars in chips you would like to purchase: ");
fgets(chipsPurchased, sizeof(chipsPurchased), stdin);
Typo: above
'
'
should be
'
using scanf() to input strings with spaces