Connecting Tech Pros Worldwide Forums | Help | Site Map

Issues with input

Spartan815
Guest
 
Posts: n/a
#1: Nov 14 '05
is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...

Peter Pichler
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Issues with input


"Spartan815" <absolutspartan@hotmail.com> wrote in message
news:d8a1d17c.0402200911.70472f06@posting.google.c om...[color=blue]
> is there another function or way of going about getting input from the
> command line besides getch().[/color]

From the command line? Wazzat? If you meant "standard input", there is a
multitude of standard functions:

[f]getc()
getchar()
fgets()
[f]scanf() /* better not */
gets() /* DEFINITELY not! */

On the other hand, there is no getch().
[color=blue]
> Right now, Im just doing a simple while
> loop (terminated by a new line character) to get input, but I also
> want to be able to save up to 10 entries so that a history can be
> display.[/color]

s/display/displayed

I am not sure I understand what you are trying to do. To save N lines
of text you need an array of N strings. You could do it dynamically
with a linked list. They are dead easy to use as a stack, an ideal
structure for keeping a history.
[color=blue]
> I know thats rather open-ended, so really my better question
> is can anyone point me to some good C resources online so I can
> actually read about it...[/color]

This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading".

Peter


CBFalconer
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Issues with input


Spartan815 wrote:[color=blue]
>
> is there another function or way of going about getting input from the
> command line besides getch(). Right now, Im just doing a simple while
> loop (terminated by a new line character) to get input, but I also
> want to be able to save up to 10 entries so that a history can be
> display. I know thats rather open-ended, so really my better question
> is can anyone point me to some good C resources online so I can
> actually read about it...[/color]

I suspect you mean the terminal, or stdin, rather than command
line. Standard routines include getc, fgetc, fgets. scanf is not
recommended for this purpose, nor is gets. However you can also
use my favorite, ggets, available on my site, download section.
R. Heathfield has another similar routine, that requires more care
and feeding IMO.

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


Malcolm
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Issues with input



"Spartan815" <absolutspartan@hotmail.com> wrote in message[color=blue]
>
> is there another function or way of going about getting input from the
> command line besides getch(). Right now, Im just doing a simple
> while loop (terminated by a new line character) to get input, but I also
> want to be able to save up to 10 entries so that a history can be
> display. I know thats rather open-ended, so really my better
> question is can anyone point me to some good C resources online so
> I can actually read about it...
>[/color]
getc() is the best function for reading from stdin. gets() is unsafe,
scanf() is difficult to use, fgets() has subtle problems on overflow.

So write this function

char *getline(void)

calls malloc() and getc() to return a line from stdin.

Now all you need is an array of character points

char *history[10];
int current = 0; /* this is not strictly needed but may make things easier
to understand */

Intitialise all elements of history to NULL. Then for the first ten lines
read in the lines, incrementing current. When current gets to ten, the array
is full. Therefore free the top entry (history[0]) and call memmove() to
move all the pointers up one place. Then add your newly read line to the
bottom.


Paul Hsieh
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Issues with input


malcolm@55bank.freeserve.co.uk says...[color=blue]
> "Spartan815" <absolutspartan@hotmail.com> wrote in message[color=green]
> > is there another function or way of going about getting input from the
> > command line besides getch(). Right now, Im just doing a simple
> > while loop (terminated by a new line character) to get input, but I also
> > want to be able to save up to 10 entries so that a history can be
> > display. I know thats rather open-ended, so really my better
> > question is can anyone point me to some good C resources online so
> > I can actually read about it...[/color]
>
> getc() is the best function for reading from stdin. gets() is unsafe,
> scanf() is difficult to use, fgets() has subtle problems on overflow.[/color]

I'll second all that. But scanf has the additional problem of necessarily
parsing your input in ways that don't allow you to definitively recover the raw
input.

I have a webpage explaining all of this, with sample code, here:

http://www.pobox.com/~qed/userInput.html
[color=blue]
> So write this function
>
> char *getline(void)
>
> calls malloc() and getc() to return a line from stdin.
>
> Now all you need is an array of character points
>
> char *history[10];
> int current = 0; /* this is not strictly needed but may make things easier
> to understand */
>
> Intitialise all elements of history to NULL. Then for the first ten lines
> read in the lines, incrementing current. When current gets to ten, the array
> is full. Therefore free the top entry (history[0]) and call memmove() to
> move all the pointers up one place. Then add your newly read line to the
> bottom.[/color]

That's one way, but why not just retain current as a modulo 10 value rather
than shifting your whole history? For example, using the code I indicated on
the webpage above, we can write simply:

char * history[10] = {NULL, ..., NULL};
int current = 0;

while (...) {
free (history[current % 10U]); /* free(NULL) is ok */
getstralloc (&(history[current % 10U]));
current++;

...

/* Dump the history */
for (i=current-10; i < current; i++) {
if (history[i % 10U]) {
printf ("%d) %s\n", i, history[i % 10U]);
}
}
}

There are ways to dramatically reduce the cost of the "%" operations, but I
will leave that as an exercise to the reader.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Spartan815
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Issues with input


> "This may sound a bit radical, but have you tried a... book? You know,[color=blue]
> the things made of processed pulp of certain plants (usually trees),
> usually rectangular in shape and glued together along the longer side
> so you can leaf through them. It is sometimes called "reading"."[/color]

-- Wow, way to be a total jerk about it. It was a simple question,
you chould have decided not to post a response at all (and in this
case it probably would have been better). Not that I should even
dignify what you said with a response but here we go...

1) I dont have a car to get to the library
2) I dont have money to go throwing around and buying books beyond the
required text books for classes
3) the text for the class is a book on operating systems -- with
little or no examples of code

and for the above reasons, I was looking for an online resource so I
could do that "reading" thing you talked about from home. You know,
here on my computer, they have those things called webpages that often
include helpful information on a wide range of topics.
Peter Pichler
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Issues with input


"Spartan815" <absolutspartan@hotmail.com> wrote in message
news:d8a1d17c.0402201558.5b0fc6fd@posting.google.c om...[color=blue][color=green]
> > "This may sound a bit radical, but have you tried a... book? You know,
> > the things made of processed pulp of certain plants (usually trees),
> > usually rectangular in shape and glued together along the longer side
> > so you can leaf through them. It is sometimes called "reading"."[/color][/color]

Please keep the attributions for the benefit of others.
[color=blue]
> -- Wow, way to be a total jerk about it.[/color]

Indeed. I've just had an unhappy love affair, so I don't see why anybody
else should have a good time ;-)
[color=blue]
> It was a simple question,[/color]

Hmm, let's see:
"is there another function or way of going about getting input from the
command line besides getch()."

First of all, it makes no sense. There is no command line and no getch()
in standard C. You got two independent answers nevertheless, all in good
intentions.
[color=blue]
> you chould have decided not to post a response at all (and in this
> case it probably would have been better).[/color]

As you wish. I will not bother the next time. But you should get used to
some level of sarcasm on this newsgroup, if you want to learn something.
[color=blue]
> 1) I dont have a car to get to the library[/color]

A bicycle or even your own feet would do.
[color=blue]
> 2) I dont have money to go throwing around and buying books beyond the
> required text books for classes[/color]

"Throwing around?" Now this must be the most arrogant response I've seen
in years. Knowledge is money, have you heard that? And knowledge /costs/
money too. If you are not willing to invest in your own education, then
do not be surprised if you don't achieve much later.
[color=blue]
> 3) the text for the class is a book on operating systems -- with
> little or no examples of code[/color]

Then get a second book. The FAQ has some recommendations. It does not
hurt to have books on several topics.
[color=blue]
> and for the above reasons, I was looking for an online resource so I
> could do that "reading" thing you talked about from home. You know,
> here on my computer, they have those things called webpages that often
> include helpful information on a wide range of topics.[/color]

That's all very nice. Howevere, a book is better on many accounts. First,
it is much more readily available. Second, you could make notes in it.
Then there is the thing about reading in bed - if you have a laptop with
wireless internet connection, then you surely have enough money to afford
a book too. And even a laptop would not be much help for reading on the
bus on the way to work or school.

I write all this with the intention to help you. If you want to take
offence, that is your choice. Message ends.

Peter


Malcolm
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Issues with input



"Paul Hsieh" <qed@pobox.com> wrote in message[color=blue]
>
> That's one way, but why not just retain current as a modulo 10 value >[/color]
rather than shifting your whole history?[color=blue]
>[/color]
As it is we only have ten pointers to move, in the context of user input.
Even an extremely slow computer will do this in under just-noticeable time,
and it keeps things simple.
However generally a circular buffer is a faster way of storing a fixed size
list. If we had many thousands of entries the OP might like to consider
doing this.



Joona I Palaste
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Issues with input


Spartan815 <absolutspartan@hotmail.com> scribbled the following:[color=blue][color=green]
>> "This may sound a bit radical, but have you tried a... book? You know,
>> the things made of processed pulp of certain plants (usually trees),
>> usually rectangular in shape and glued together along the longer side
>> so you can leaf through them. It is sometimes called "reading"."[/color][/color]
[color=blue]
> -- Wow, way to be a total jerk about it. It was a simple question,
> you chould have decided not to post a response at all (and in this
> case it probably would have been better).[/color]

*PLONK*

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Closed Thread