Radith wrote:
In follow-up to my previous post;
Would any one know how I can intake sentences of text (i.e. With space
characters); because scanf("%s") causes input to terminate after a space
character.
Cheers.
You can use the %[ conversion specifier to read everything up to a
particular delimiter; for example
scanf("%[^\n]", buf);
will read everything (including spaces, tabs, and other non-newline
whitespace characters) up to but not including the next newline
character. Since this sets up the possibility for a buffer overflow
(for example, buf is sized to hold 20 characters plus the terminator,
but there are 50 characters before the next newline), you need to
specify a field width in the conversion specifier:
scanf("%20[^\n]", buf)
So far, so good, but now we have a situation where we may have leftover
characters in the input stream; going by our scenario above, if we had
50 characters in the input stream, after that scanf() we now have 30
characters left over. If you're expecting that all input is to be
consumed after each scanf() operation, then you need some way to
dispose of those extra characters. We can use the same conversion
specifier again, but instead of specifying a field width, we use '*',
which will cause the input to be read but not assigned:
scanf("%20[^\n]%*[^\n]", buf);
So this will read up to 20 non-newline characters and assign them to
buf, and any remaining non-newline characters will be read and
discarded. The newline character is still stuck in the input stream,
however, and I can never remember the magic format string that will
cause it to be consumed -- doing
scanf("%20[^\n]%*[^\n]\n", buf);
causes my test program to hang, waiting for me to type additional
newline characters before returning. So at this point I typically give
up and just use fgets():
fgets(buf, sizeof buf, stdin);
This basically accomplishes the same thing as the second to last
scanf() except that the newline character is read and stored to buf (if
there's room), and to get rid of that newline I use something like
if (strchr(buf, '\n'))
*strchr(buf, '\n') = 0;
Like the scanf() call above, if there are more input characters in the
stream than the buffer is sized to hold, then they have to be consumed
somehow. I typically make successive calls to fgets(), testing for the
presence of the newline character:
if (fgets(buf, sizeof buf, stdin))
{
if (strchr(buf, '\n'))
{
*strchr(buf, '\n') = 0;
}
else
{
char junk[80];
do
{
if (!fgets(junk, sizeof junk, stdin))
{
/* handle EOF or read error */
break;
}
} while (!strchr(junk, '\n'));
}
}
else
{
/* EOF or read error */
}
Lately, though, I've taken the approach where I'll try to save the
entire input line regardless of how long it is, so I call fgets(buf,
sizeof buf, stdin) repeatedly, appending the contents of buf to a
dynamically allocated buffer that's extended via realloc with each
read. Of course, this adds memory management issues to the mix (who's
responsible for freeing the memory after it's used, what happens if
realloc() fails while extending the buffer, etc.), so it's a bit more
work.
And all you wanted to do was read a sentence with whitespace in it.
Welcome to the wonderful world of C text processing.