keyongtech


  keyongtech > c > 10/2008

 #1  
09-10-08, 11:08 AM
arnuld
I searched the c.l.c archives provided by Google as Google Groups with
"word input" as the key words and did not come up with anything good.


C++ has std::string for taking a word as input from stdin. C takes input
in 2 ways:

1) as a character, etchar()
2) as a whole line, fgets()


as C programmer, are we supposed to create a get_word function everytime
when we need a words as input from stdin ( e.g. terminal)
 #2  
09-10-08, 11:24 AM
vippstar
On Sep 10, 1:08 pm, arnuld <sunr...@invalid.address> wrote:
> I searched the c.l.c archives provided by Google as Google Groups with
> "word input" as the key words and did not come up with anything good.
>
> C++ has std::string for taking a word as input from stdin. C takes input
> in 2 ways:
>
> 1) as a character, etchar()
> 2) as a whole line, fgets()
>
> as C programmer, are we supposed to create a get_word function everytime
> when we need a words as input from stdin ( e.g. terminal)


char word[64];
scanf("%63s", word);

Alternatively, write a get_line function (or use one written by pete,
richard heathfield, eric sossman, cbfalconer et cetera) and then split
that into words.
 #3  
09-10-08, 04:14 PM
Richard Bos
arnuld <sunrise> wrote:

> C++ has std::string for taking a word as input from stdin. C takes input
> in 2 ways:
>
> 1) as a character, etchar()
> 2) as a whole line, fgets()
>
> as C programmer, are we supposed to create a get_word function everytime
> when we need a words as input from stdin ( e.g. terminal)


There is no generic solution (mainly because there is no consensus on
what a "word" is), so yes.

Richard
 #4  
09-10-08, 09:34 PM
Malcolm McLean
"arnuld" <sunrise> wrote in message
> as C programmer, are we supposed to create a get_word function everytime
> when we need a words as input from stdin ( e.g. terminal)
>

Generally there will be a regular expression parser available. It's not part
of the standard library, unfortunately, so the details may vary.
You can specify exactly what you mean by a 'word', and extract with that.
 #5  
09-10-08, 10:50 PM
Pilcrow
On Wed, 10 Sep 2008 15:08:40 +0500, arnuld <sunrise>
wrote:

>I searched the c.l.c archives provided by Google as Google Groups with
>"word input" as the key words and did not come up with anything good.
>>C++ has std::string for taking a word as input from stdin. C takes input

>in 2 ways:
>
> 1) as a character, etchar()
> 2) as a whole line, fgets()
>>as C programmer, are we supposed to create a get_word function everytime

>when we need a words as input from stdin ( e.g. terminal)


Try using fgets(), and strtok(). strtok() will allow you to define word
separators to your taste.

Here is sample code:

------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#define MAXLINE 500

char *tok;
char line[MAXLINE];

int main(void)
{
while(fgets(line, MAXLINE, stdin) != NULL) {
if((tok = strtok(line," \n")) != NULL) puts(tok); /* first
token on each line */
while((tok = strtok(NULL," \n")) != NULL) puts(tok); /*
subsequent tokens */
}
return 0;
}
 #6  
09-10-08, 11:01 PM
Gordon Burditt
>I searched the c.l.c archives provided by Google as Google Groups with
>"word input" as the key words and did not come up with anything good.
>>C++ has std::string for taking a word as input from stdin. C takes input

>in 2 ways:
>
> 1) as a character, etchar()
> 2) as a whole line, fgets()
>>as C programmer, are we supposed to create a get_word function everytime

>when we need a words as input from stdin ( e.g. terminal)


The first step is to define what a "word" is.

How many words are these:

1. don't
2. antidisestablish-
mentarianism
3. Joe,Bob,Sally, and Henry.
4. Joe, Bob, Sally, and Henry.
5. $1,416,383,583.20
6. ()@#$#^&*#^%%#^@*^$&*$
7. George W. Bush
8. slam-dunk
9. 15th-century vase
10. M.O.N.S.T.E.R., the computer chess-playing machine
11. lord.high.master.of.the.universe.and.dns

Justify your answers.
 #7  
09-10-08, 11:05 PM
Richard Heathfield
Pilcrow said:

> On Wed, 10 Sep 2008 15:08:40 +0500, arnuld <sunrise>
> wrote:


<snip>

>>as C programmer, are we supposed to create a get_word function everytime
>>when we need a words as input from stdin ( e.g. terminal)

>
> Try using fgets(), and strtok(). strtok() will allow you to define word
> separators to your taste.


This is poor advice for a beginner. Whilst strtok does have its uses, it
also has issues - traps for the unwary programmer. These derive from its
maintenance of significant state between calls, which makes it unsuitable
for use in library functions.
 #8  
09-11-08, 12:06 AM
Pilcrow
On Wed, 10 Sep 2008 22:05:46 +0000, Richard Heathfield
<rjh> wrote:

>Pilcrow said:
>><snip>
>>This is poor advice for a beginner. Whilst strtok does have its uses, it

>also has issues - traps for the unwary programmer. These derive from its
>maintenance of significant state between calls, which makes it unsuitable


I understood that, and I am a 'beginner'. It is very adequately covered
in textbooks (see 'C in a Nutshell', ISBN 0-596-00697-7, page 440),
somewhat less so in K&R2. And I gave the questioner an example to help
him. My dissatisfaction with strtok() is that repeated separation
characters are treated as one, making it difficult to present the user
with an intuitively understandable interface. It is not usually a good
idea to equate ignorance and stupidity.
 #9  
09-11-08, 01:01 AM
CBFalconer
arnuld wrote:
>
> I searched the c.l.c archives provided by Google as Google Groups
> with "word input" as the key words and did not come up with
> anything good.
>
> C++ has std::string for taking a word as input from stdin. C takes
> input in 2 ways:
>
> 1) as a character, etchar()
> 2) as a whole line, fgets()
>
> as C programmer, are we supposed to create a get_word function
> everytime when we need a words as input from stdin ( e.g. terminal)


Well, first you have to define a word. Does it terminate on
blanks, on blanks and non-print chars, on blanks and tabs, etc. I
think you will find that the C++ mechanism terminates on blanks and
'\n' (but I could well be wrong). Having defined it, you just
write the code to extract such a beast from a stream (or from a
string). At that point both you and your code reader know exactly
what the function extracts.

Don't forget to preserve the exit char. Something else may need
it.

Note that, having written the function, you are allowed to keep its
source (and its object code) and reuse it as often as you wish,
with minimum effort. If you have taken the elementary precaution
of writing it in standard C, you can use it anywhere.
 #10  
09-11-08, 05:43 AM
William Pursell
On 10 Sep, 11:08, arnuld <sunr...@invalid.address> wrote:

>
> as C programmer, are we supposed to create a get_word function everytime
> when we need a words as input from stdin ( e.g. terminal)


No. You should either find a function that does what you want
or write it yourself, and once you have done that...don't
ever do it again. Put it in a library and use it.

If you are re-writing the same function repeatedly, then
you aren't a C-programmer. You aren't any kind of programmer.
Re-writing the same functionality can be a useful
exercise for the novice, but it is a silly waste of
time otherwise.
 #11  
09-11-08, 07:24 AM
arnuld
> On Wed, 10 Sep 2008 17:01:53 -0500, Gordon Burditt wrote:

> The first step is to define what a "word" is.


Fore *my* program, a word is a collection of letters, numbers or anything
separated by space, tab or newline.


> How many words are these:
>
> 1. don't


1 word


> 2. antidisestablish-mentarianism


1 word


> 3. Joe,Bob,Sally, and Henry.


3 words. Joe,Bob,Sally, makes one word, and makes second, Henry. makes
3rd ( notice that full stop with Henry.)


> 4. Joe, Bob, Sally, and Henry.


5 words


> 5. $1,416,383,583.20


all 1 word. There is no space in between them.


> 6. ()@#$#^&*#^%%#^@*^$&*$


1 word


> 7. George W. Bush


3 words



> 8. slam-dunk


1 word


> 9. 15th-century vase


2 words


> 10. M.O.N.S.T.E.R., the computer chess-playing machine


5 words



> 11. lord.high.master.of.the.universe.and.dns


1 word, of course



> Justify your answers.



Any collection of letters,symbols or numbers separated by single or
multiple spaces or tab or newline. Therefore

comp.lang.c++ --> 1 word
Std. Lib --> 2 words
Lov@389&om --> 1 word


I think it is pretty much clear now what a word is.
 #12  
09-11-08, 10:36 AM
Bartc
"arnuld" <sunrise> wrote in message
news:0929
>> Fore *my* program, a word is a collection of letters, numbers or anything

> separated by space, tab or newline.
>>

> 1 word
>>

> 1 word
>>

> 3 words. Joe,Bob,Sally, makes one word, and makes second, Henry. makes
> 3rd ( notice that full stop with Henry.)


You have commas in the middle of words?

Ever heard of comma-delimited files? Comma is way up there with space and
tab.
 #13  
09-11-08, 10:49 AM
arnuld
> On Thu, 11 Sep 2008 09:36:49 +0000, Bartc wrote:

> You have commas in the middle of words?
>
> Ever heard of comma-delimited files? Comma is way up there with space and
> tab.



yes, I know and @%$@programmimnng34 is not a word either. If I start to
differentiate these things then it will become very complex to define what
a word is and there could be lots of controversy over what should be (or
could be ?) a word. So I take a simple approach, the white space
(whether a newline or a tab or a single space) separates the words. simple ...
 #14  
09-11-08, 12:24 PM
arnuld
> On Wed, 10 Sep 2008 20:01:33 -0400, CBFalconer wrote:

> Well, first you have to define a word. Does it terminate on
> blanks, on blanks and non-print chars, on blanks and tabs, etc. I
> think you will find that the C++ mechanism terminates on blanks and
> '\n' (but I could well be wrong).


I have told this already in my last reply ( to BartC )



> Having defined it, you just
> write the code to extract such a beast from a stream (or from a
> string). At that point both you and your code reader know exactly
> what the function extracts.


Now there is a big problem in this. In C++ i don't have to care whether
users enter one word or 100s. Memory was being managed by std. lib.
vector. Now here I am thinking of using fgets() to store the input,
which has 2 problems:

1) extract words from each line.
2) fgets() uses array top store data and I don't know how large is
the input, so I can't decide on the size of the array.




> Don't forget to preserve the exit char.
> Something else may need it.


you mean null character ?


> Note that, having written the function, you are allowed to keep its
> source (and its object code) and reuse it as often as you wish, with
> minimum effort. If you have taken the elementary precaution of writing
> it in standard C, you can use it anywhere.


Thats what I want to do, write in ANSI C :)
 #15  
09-11-08, 01:00 PM
Richard Heathfield
arnuld said:

<snip>

> Now here I am thinking of using fgets() to store the input,
> which has 2 problems:
>
> 1) extract words from each line.
> 2) fgets() uses array top store data and I don't know how large is
> the input, so I can't decide on the size of the array.


This is a common problem - so common, in fact, that I wrote it up on the
Web. Take a look at http://www.cpax.org.uk/prg/writings/fgetdata.php which
looks at scanf, gets, and fgets, points out the difficulties with each,
and then discusses a possible solution to the problem of arbitrarily long
lines.

On that page, I present code for reading a word at a time, and for reading
a line at a time. In fact, since you supply your own delimiters, reading a
line is really just a special case of reading a word!

I do not pretend that my code is perfect. For example, the return values
could have been better chosen (I must fix that one day).

It is not intended to be a plug-in solution to the problem (although some
people do actually use it that way and, as far as I'm aware, no harm has
come to them as a result). Rather, it is intended to demonstrate one
possible approach to the problem, in the hope that the reader will have an
"aha!" moment and perhaps come up with a solution that fits his own needs
much better than a generic solution is likely to be able to do.

Several other approaches apart from the one I chose to demonstrate are also
discussed (but not demonstrated), the intent being to give a wider view of
various ways to tackle this problem, depending on your priorities.

Finally, the page provides links to a few other people's demonstrations of
how to solve this problem, again with the intent of providing a wider
perspective on different approaches.

Similar Threads
Best way to input from stdin?

I'm writing a program that supports input from stdin. To be able to do that I tend to rely on a simple loop that tests the return of fgets(), such as the following...

How to accept input from stdin?

Hi, I try to make a wrapper around an existing program, which would behave exactly the same as the original one. But my following attempt was failed. Would you pleaes let me...

Input using stdin

How can I give input to a program using STDIN Suppose I want the program to take the value x=10 On some other site i found it as STDIN.read,but its not working.

getting input from stdin

Hi Im new to unix scripting and now Im trying to get user input from stdin and this is what I did echo "enter your name: " read name and it will run with the pointer to...

Checking available input on stdin

I know this has probably come up frequently, but couldn't find a satisfactory reference... I have some code which needs to read from stdin but must not block waiting for...


All times are GMT. The time now is 11:18 PM. | Privacy Policy