C program under Linux ------ Menu under the console

zhaozj2021-02-16  102

#include

#include

Char * menu [] = {"a - add new record",

"D - delete a record",

"q - quit", null,};

Int getchoice (Char * Greet, Char * Choices [], File * in, File * OUT)

{

INT chosen = 0;

INT SELECTED, SELECTED1;

Char ** Option;

Do {

FPrintf (OUT, "Choice:% S / N", GREET);

Option = choices;

While (* Option)

{

FPRINTF (OUT, "% S / N", * Option );

}

Do {

SELECTED = FGETC (in);

SELECTED1 = FGETC (IN);

WHILE (SELECTED1! = '/ n');

Option = choices;

While (* Option)

{

IF (SELECTED == * Option [0])

{

CHOSEN = 1; Break;

}

Option ;

}

IF (! chosen)

{

FPrintf (Out, "IncorRect Choice, SELECT AGAIN! / N");

}

} while (! ";

Return SELECTED;

}

int main ()

{

INT choice = 0;

FILE * INPUT; File * Output;

IF (! isatty (fileno (stdout)))

{

FPRINTF (stderr, "you are not a terminal, ok, / n");

}

Input = fopen ("/ dev / tty", "r");

Output = fopen ("/ dev / tty", "w");

IF (! Input ||! Output)

{

FPRINTF (stderr, "unable to open / dev / tty / n");

Exit (1);

}

Do {

Choice = Getchoice ("please select a action", menu, input, output;

FPrintf (Output, "You Have Chosen:% C / N", Choice);

} while (choice! = 'q');

exit (0);

}

转载请注明原文地址:https://www.9cbs.com/read-10782.html

New Post(0)