Re: Issues with input

From: Paul Hsieh (qed_at_pobox.com)
Date: 02/21/04


Date: Fri, 20 Feb 2004 23:33:19 GMT

malcolm@55bank.freeserve.co.uk says...
> "Spartan815" <absolutspartan@hotmail.com> wrote in message
> > 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...
>
> getc() is the best function for reading from stdin. gets() is unsafe,
> scanf() is difficult to use, fgets() has subtle problems on overflow.

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

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/


Relevant Pages

  • Re: Editable input from the console windows
    ... redirection was going somewhere else. ... I learning the syntax and build upon a previously working script. ... My case, read, and history statements are mucked up. ... # The code block that the populate the array with id3tag data has been remove. ...
    (comp.unix.shell)
  • Re: Save Array to Disk and Restore?
    ... The history is an array of SQL statements and meta statements. ... puts $f [list set theArray$theArray]} ...
    (comp.lang.tcl)
  • Re: How to pass an array of single values to a message?
    ... The OleControl prompt "data is not the correct type". ... I suspect the error is caused by the data type in the array. ... The vendor of the ActiveX control has some sample of using the control. ... However these sample code is in VB and Visual C++. ...
    (microsoft.public.fox.programmer.exchange)
  • Save Array to Disk and Restore?
    ... The history is an array of SQL statements and meta statements. ... gets stdin response ...
    (comp.lang.tcl)
  • Re: Issues with input
    ... calls mallocand getc() to return a line from stdin. ... Now all you need is an array of character points ... Intitialise all elements of history to NULL. ...
    (comp.lang.c)