473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

...Then what can I USE

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
17 1564
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
"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
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
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
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

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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: avital | last post by:
Hi, I have a sql query with cases. I need to add a condition that if hasamafactor=5 then display only cases m11-m14 else display the rest. Of course sum ( kamut) as total4mosad has to be only...
19
by: GMKS | last post by:
Hello all, I have 13 check boxes on a form. I am trying to check all the check boxes to determine if they are true or false when I close the form. At present only the first IF...Then...Else...
1
by: Dan H. | last post by:
Hello, I have an application that requires a collection that is sorted by a double field first, and then by a long field. So, lets say I have an class that has 2 fields called time, priority. I...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
4
by: ECathell | last post by:
I had read an article at one time that suggested a pattern to get around deeply nested if..then..else hell... Can anyone point me in that direction? select case statements wont work for me in...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
14
by: lawjake | last post by:
I am having a dispute at work over endifs, and cannot find any treatise on this. So I am hoping that I can get a lot of correspondece confirming my belief. Here it is: I believe the following:...
4
by: =?Utf-8?B?WmFyYm9yZw==?= | last post by:
I'm writing a little program that will run when a user logs in, checks their password expiration and also installs a piece of monitoring software if necessary. The program has to run on Vista so I...
15
by: jzakiya | last post by:
I'm translating a program in Python that has this IF Then chain IF x1 < limit: --- do a --- IF x2 < limit: --- do b --- IF x3 < limit: --- do c --- .----- ------ IF x10 < limt: ---...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.