old-www/HOWTO/NCURSES-Programming-HOWTO/scanw.html

325 lines
5.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Input functions</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE=" NCURSES Programming HOWTO "
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Output functions"
HREF="printw.html"><LINK
REL="NEXT"
TITLE="Attributes"
HREF="attrib.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>NCURSES Programming HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="printw.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="attrib.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SCANW"
></A
>7. Input functions</H1
><P
>Well, printing without taking input, is boring. Let's see functions which
allow us to get input from user. These functions also can be divided into
three categories.</P
><P
></P
><OL
TYPE="1"
><LI
><P
>getch() class: Get a character</P
></LI
><LI
><P
>scanw() class: Get formatted input</P
></LI
><LI
><P
>getstr() class: Get strings</P
></LI
></OL
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="GETCHCLASS"
></A
>7.1. getch() class of functions</H2
><P
>These functions read a single character from the terminal. But there are several
subtle facts to consider. For example if you don't use the function cbreak(),
curses will not read your input characters contiguously but will begin read them
only after a new line or an EOF is encountered. In order to avoid this, the
cbreak() function must used so that characters are immediately available to your
program. Another widely used function is noecho(). As the name suggests, when
this function is set (used), the characters that are keyed in by the user will
not show up on the screen. The two functions cbreak() and noecho() are typical
examples of key management. Functions of this genre are explained in the
<A
HREF="keys.html"
>key management section </A
>.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="SCANWCLASS"
></A
>7.2. scanw() class of functions</H2
><P
>These functions are similar to <TT
CLASS="LITERAL"
>scanf()</TT
> with the
added capability of getting the input from any location on the screen.</P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="SCANWMVSCANW"
></A
>7.2.1. scanw() and mvscanw</H3
><P
>The usage of these functions is similar to that of
<TT
CLASS="LITERAL"
>sscanf()</TT
>, where the line to be scanned is
provided by <TT
CLASS="LITERAL"
>wgetstr()</TT
> function. That is, these
functions call to <TT
CLASS="LITERAL"
>wgetstr()</TT
> function(explained
below) and uses the resulting line for a scan. </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="WSCANWMVWSCANW"
></A
>7.2.2. wscanw() and mvwscanw()</H3
><P
>These are similar to above two functions except that they read from a window,
which is supplied as one of the arguments to these functions. </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="VWSCANW"
></A
>7.2.3. vwscanw()</H3
><P
>This function is similar to <TT
CLASS="LITERAL"
>vscanf()</TT
>. This can
be used when a variable number of arguments are to be scanned.</P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="GETSTRCLASS"
></A
>7.3. getstr() class of functions</H2
><P
>These functions are used to get strings from the terminal. In essence, this
function performs the same task as would be achieved by a series of calls to
<TT
CLASS="LITERAL"
>getch()</TT
> until a newline, carriage return, or
end-of-file is received. The resulting string of characters are pointed to by
<TT
CLASS="LITERAL"
>str</TT
>, which is a character pointer provided by
the user.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="GETSTREX"
></A
>7.4. Some examples</H2
><DIV
CLASS="EXAMPLE"
><A
NAME="BSCEX"
></A
><P
><B
>Example 4. A Simple scanw example </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
><SPAN
CLASS="INLINEMEDIAOBJECT"
>#include &#60;ncurses.h&#62; /* ncurses.h includes stdio.h */
#include &#60;string.h&#62;
int main()
{
char mesg[]="Enter a string: "; /* message to be appeared on the screen */
char str[80];
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
/* print the message at the center of the screen */
getstr(str);
mvprintw(LINES - 2, 0, "You Entered: %s", str);
getch();
endwin();
return 0;
}</SPAN
></PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="printw.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="attrib.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Output functions</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Attributes</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>