By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,031 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,031 IT Pros & Developers. It's quick & easy.

...Then what can I USE

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Radith wrote:
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.


How do you define a sentence? For strings of characters up to EOL, use
fgets. For strings of characters ending with a dot (period), question
mark or exclamation mark - I don't know about Dan Pop (the resident
scanf expert), but I would write my own function.

Peter

Nov 15 '05 #2

P: n/a
"Radith" <ra****@xtra.co.nz> writes:
In follow-up to my previous post;
Then please post it as a followup, not as a new article.
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.


fgets(). You should be able to find documentation on it on your
system or in any decent textbook.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3

P: n/a
Radith wrote on 31/07/05 :
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.


Use fgets() to get a line of text.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #4

P: n/a
Radith wrote:

In follow-up to my previous post;
Then post as a reply to your previous, and quote it adequately.

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.


However this stands by itself. My suggestion is to use ggets,
which is available in portable source form at:

<http://cbfalconer.home.att.net/download/ggets.zip>

with some examples of use. It handles memory allocation, and all
you have to do is pass the resulting string to free when you are
done with it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #5

P: n/a
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.


Because I often wanted input from keyboard and liked Java's
StringBuffer, I wrote my own.. It is a triviak task and I suggest you do
the same - or use someone else's...

--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #6

P: n/a

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.

Nov 15 '05 #7

P: n/a
fgets() ?

<posted & mailed>

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.


--
Remove '.nospam' from e-mail address to reply by e-mail
Nov 15 '05 #8

P: n/a
John Bode wrote:
Radith wrote:

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.

.... snip ...
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.


As I pointed out earlier, you can get code [ggets()] that already
does all this from my page. Like gets, it consistently returns a
string with the final \n removed and does not require a size
parameter. Unlike gets it will never overwrite any storage. The
only thing to remember is that you have to free the storage when
done with it. Completely portable.

Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #9

P: n/a
CBFalconer wrote:
Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>


Q: Why this
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */ ?

--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #10

P: n/a
Giannis Papadopoulos wrote:
CBFalconer wrote:
Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>


Q: Why this
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */ ?


Because it is sufficient to handle most interactive input. The -16
avoids allocating total space (usable + overhead) above a power of
2, to reduce wastage in at least one malloc system familiar to me
(my own). It is not critical in any way.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #11

P: n/a
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.


/*
** If rc equals 0, then an empty line was entered
** and the array contains garbage.
** If rc equals EOF, then the end of file was reached.
** If rc equals 1, then there is a string in array.
/*
/* BEGIN new.c */

#include <stdio.h>

#define LENGTH 30
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

fputs("Enter a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string is:%s\n\n"
"Hit the Enter key to end,\nor enter "
"another string to continue:", array);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

/* END new.c */

--
pete
Nov 15 '05 #12

P: n/a
In article <Tq*******************@news.xtra.co.nz>, 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.


fgets()
But if it's for interaction (ie a user interface) readline() might be
better suited.

don't use gets() - it's toxic.

Bye.
Jasen
Nov 15 '05 #13

P: n/a
CBFalconer <cb********@yahoo.com> writes:
Giannis Papadopoulos wrote:
CBFalconer wrote:
Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>


Q: Why this
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */ ?


Because it is sufficient to handle most interactive input. The -16
avoids allocating total space (usable + overhead) above a power of
2, to reduce wastage in at least one malloc system familiar to me
(my own). It is not critical in any way.


Sounds like a harmless but relatively unnecessary optimization, since
other malloc() implementations may use pools of preallocated "metadata"
structures in an area quite different from the usable data itself ;-)

Thanks for the explanation though. This had caught my eye too and I
wondered about it for a while, until I saw the post of Giannis.

Nov 15 '05 #14

P: n/a
Jasen Betts <ja***@clunker.homenet> writes:
In article <Tq*******************@news.xtra.co.nz>, 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.


fgets()
But if it's for interaction (ie a user interface) readline() might be
better suited.


There is no readline() function in standard C.

<OT>
There is a readline package available from GNU. The licensing terms
may be an issue, but they're off-topic here.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #15

P: n/a
Keith Thompson wrote:
Jasen Betts <ja***@clunker.homenet> writes:

.... snip ...

fgets()
But if it's for interaction (ie a user interface) readline()
might be better suited.


There is no readline() function in standard C.

<OT>
There is a readline package available from GNU. The licensing
terms may be an issue, but they're off-topic here.
</OT>


However I have put my ggets() function in public domain.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 15 '05 #16

P: n/a
In article <dc**********@volcano1.grnet.gr>, Giannis Papadopoulos wrote:
CBFalconer wrote:
Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>


Q: Why this
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */ ?


It's an ms-dos thing... (actually a comanality among ms-dos (and other real-mode?) c programming
enviroments, not part of the OS itself)

-

Bye.
Jasen
Nov 15 '05 #17

P: n/a
Jasen Betts wrote:
Giannis Papadopoulos wrote:
CBFalconer wrote:
Repeated: <http://cbfalconer.home.att.net/download/ggets.zip>


Q: Why this
#define INITSIZE 112 /* power of 2 minus 16, helps malloc */ ?


It's an ms-dos thing... (actually a comanality among ms-dos (and
other real-mode?) c programming enviroments, not part of the OS
itself)


Nonsense. I repeat the explanation I gave Giannis:

Because it is sufficient to handle most interactive input. The -16
avoids allocating total space (usable + overhead) above a power of
2, to reduce wastage in at least one malloc system familiar to me
(my own). It is not critical in any way.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.