...Then what can I USE

R

Radith

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

Peter Pichler

Radith said:
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
 
K

Keith Thompson

Radith said:
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.
 
C

CBFalconer

Radith said:
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.
 
G

Giannis Papadopoulos

Radith said:
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.
 
J

John Bode

Radith said:
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.
 
J

James McIninch

fgets() ?

<posted & mailed>
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.
 
C

CBFalconer

John said:
.... 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>
 
C

CBFalconer

Giannis said:
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.
 
P

pete

Radith said:
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 */
 
J

Jasen Betts

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
 
G

Giorgos Keramidas

CBFalconer said:
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.
 
K

Keith Thompson

Jasen Betts said:
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>
 
C

CBFalconer

Keith said:
.... snip ...

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

Jasen Betts

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
 
C

CBFalconer

Jasen said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
Best cryptoconsultant

Latest Threads

Top