bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New to C, ncurses and a big task assigned.


From: daniel silva ferreira bruno
Subject: Re: New to C, ncurses and a big task assigned.
Date: Sun, 11 Nov 2012 03:32:09 -0800 (PST)

Hi, Shailendra.

    I tested your code and I found that:

1) In the functions "void write_data0()", "void write_data1()", "void write_data2()", "void write_data3()", "void write_data4()", "void write_data5()", "void write_data6()", "void write_data7()", the conditional statements:

if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1;  }

and

if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }

are incorrect, because the lines "return 1;" should be "return ;" in these statements.

2) The statements, in the functions "void write_data0()", "void write_data1()", "void write_data2()", "void write_data3()", "void write_data4()", "void write_data5()", "void write_data6()", "void write_data7()":

"
FILE *fp,*ft;
 fp = fopen("file.txt", "w");
 ft = fopen("servertype.txt","w");
 if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return ;  }
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return ; }
"

should be in a different "void" function, because it could reduce the total line numbers of this code with this function.

Best Regards,

                   Daniel Bruno.

--- Em ter, 6/11/12, shailendra singh <address@hidden> escreveu:

De: shailendra singh <address@hidden>
Assunto: Re: New to C, ncurses and a big task assigned.
Para: address@hidden
Data: Terça-feira, 6 de Novembro de 2012, 15:01

i am including my code with this mail.Its a big code,for a programmer like me who is very new in this field,it quite difficult to optimise it.
I need to do 2 changes in this code.I need to bring the menu in centre of the screen and change the options of second menu.Along with that in the end of the program,when any of the condition selected = 8 or 0 or 1 is right execute the statement and delete theGUI and return a simple console window with output.Also i am unable to execute telnet and localhost command or any net based command with this.MAy be there is something wrong with fork() and execlp().
#include <curses.h>
#include <stdlib.h>
#include<stdio.h>

#define ENTER 10
#define ESCAPE 27
void init_curses()
{
        initscr();
        start_color();
        init_pair(1,COLOR_WHITE,COLOR_BLUE);
        init_pair(2,COLOR_BLUE,COLOR_WHITE);
        init_pair(3,COLOR_RED,COLOR_WHITE);
        curs_set(0);
        //noecho();
        keypad(stdscr,TRUE);
}
void draw_menubar(WINDOW *menubar)
{
        wbkgd(menubar,COLOR_PAIR(2));
        waddstr(menubar,"PARALLEL");
        wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"CON.");
        wattroff(menubar,COLOR_PAIR(3));
        wmove(menubar,0,20);
        waddstr(menubar,"SERIAL");
        wattron(menubar,COLOR_PAIR(3));
        waddstr(menubar,"CON.");
        wattroff(menubar,COLOR_PAIR(3));
}
WINDOW **draw_menu(int start_col)
{
      
        WINDOW **items;
        items=(WINDOW **)malloc(9*sizeof(WINDOW *));

        items[0]=newwin(10,19,1,start_col);
        wbkgd(items[0],COLOR_PAIR(2));
        box(items[0],ACS_VLINE,ACS_HLINE);
        items[1]=subwin(items[0],1,17,2,start_col+1);
wprintw(items[1],"Shailendra singh",1);
        items[2]=subwin(items[0],1,17,3,start_col+1);
wprintw(items[2],"THIVNENT TECH",2);
        items[3]=subwin(items[0],1,17,4,start_col+1);
wprintw(items[3],"THINCLIENT",3);
        items[4]=subwin(items[0],1,17,5,start_col+1);
wprintw(items[4],"SOLARCOMPUTING",4);
        items[5]=subwin(items[0],1,17,6,start_col+1);
wprintw(items[5],"TECHNOCRATS",5);
        items[6]=subwin(items[0],1,17,7,start_col+1);
wprintw(items[6],"SENSORS",6);
        items[7]=subwin(items[0],1,17,8,start_col+1);
wprintw(items[7],"NCURSESLIB",7);
        items[8]=subwin(items[0],1,17,9,start_col+1);
wprintw(items[8],"GCC COMPILER",8);

       

       
               
        wbkgd(items[1],COLOR_PAIR(1));
        wrefresh(items[0]);
        return items;
}

void write_data0()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
 if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1;  }
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.0 \n", fp);
fputs("Server type = R ",ft); 
fclose(ft);
fclose(fp); 
}
void write_data1()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
 if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1; }
  if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                 return 1;}
fputs("172.16.254.1 \n", fp); 
fputs("Server type = T ",ft); 
fclose(ft);
fclose(fp); 
}

void write_data2()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w");  
 ft = fopen("servertype.txt","w");
if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                return 1;}
if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }

fputs("172.16.254.2 \n", fp); 
fputs("Server type = C ",ft); 
fclose(ft);
fclose(fp); 
}

void write_data3()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
 if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1; }
  if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.3 \n", fp);
fputs("Server type = R ",ft); 
fclose(ft);
fclose(fp); 
}

void write_data4()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1;}
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.4 \n", fp); 
fputs("Server type = T ",ft); 
fclose(ft);
fclose(fp); 
}
void write_data5()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1; }
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.5 \n", fp);
fputs("Server type = C ", ft); 
fclose(ft); 
fclose(fp); 
}
void write_data6()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                 return 1;}
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.6 \n", fp); 
fputs("Server type = R ",ft); 
fclose(ft);
fclose(fp); 
}
void write_data7()
{FILE *fp,*ft;
 fp = fopen("file.txt", "w"); 
 ft = fopen("servertype.txt","w");
if(fp == NULL) {fprintf(stderr, "Cannot open \"file.txt\" for appending\n");
                return 1; }
 if(ft == NULL) {fprintf(stderr, "Cannot open \"servertype.txt\" for appending\n");
                return 1; }
fputs("172.16.254.7 \n", fp); 
fputs("Server type = T ",ft); 
fclose(ft);
fclose(fp);
}


WINDOW **draw_menu1(int start_col)
{
      
        WINDOW **items;
        items=(WINDOW **)malloc(9*sizeof(WINDOW *));

        items[9]=newwin(10,19,1,start_col);
        wbkgd(items[9],COLOR_PAIR(2));
        box(items[9],ACS_VLINE,ACS_HLINE);
        items[1]=subwin(items[9],1,17,2,start_col+1);
wprintw(items[1],"SHAILENDRA SINGH",1);
        items[2]=subwin(items[9],1,17,3,start_col+1);
wprintw(items[2],"thivnent tech",2);
        items[3]=subwin(items[9],1,17,4,start_col+1);
wprintw(items[3],"THINCLIENT",3);
        items[4]=subwin(items[9],1,17,5,start_col+1);
wprintw(items[4],"SOLARCOMPUTING",4);
        items[5]=subwin(items[9],1,17,6,start_col+1);
wprintw(items[5],"TECHNOCRATS",5);
        items[6]=subwin(items[9],1,17,7,start_col+1);
wprintw(items[6],"SENSORS",6);
        items[7]=subwin(items[9],1,17,8,start_col+1);
wprintw(items[7],"NCURSESLIB",7);
        items[8]=subwin(items[9],1,17,9,start_col+1);
wprintw(items[8],"GCC COMPILER",8);

       

       
               
        wbkgd(items[1],COLOR_PAIR(1));
        wrefresh(items[9]);
        return items;
}
void delete_menu(WINDOW **items,int count)
{
        int i;
        for (i=0;i<count;i++)
                delwin(items[i]);
        free(items);
}
int scroll_menu(WINDOW **items,int count,int menu_start_col)
{
        int key;
        int selected=0;
        while (1) {
                key=getch();
                if (key==KEY_DOWN || key==KEY_UP) {
                        wbkgd(items[selected+1],COLOR_PAIR(2));
                        wnoutrefresh(items[selected+1]);
                        if (key==KEY_DOWN) {
                                selected=(selected+1) % count;
                        } else {
                                selected=(selected+count-1) % count;
                        }
                        wbkgd(items[selected+1],COLOR_PAIR(1));
                        wnoutrefresh(items[selected+1]);
                        doupdate();
                } else if (key==KEY_LEFT || key==KEY_RIGHT) {
                        delete_menu(items,count+1);
                        touchwin(stdscr);
                        refresh();
                        items=draw_menu(20-menu_start_col);
                        return scroll_menu(items,8,20-menu_start_col);
                } else if (key==ESCAPE) {
                        return -1;
                } else if (key==ENTER) {
                        return selected;
                }
        }
}
int main()
{
    int key;
    WINDOW *menubar,*messagebar;
   
    init_curses();
   
    bkgd(COLOR_PAIR(1));
    menubar=subwin(stdscr,1,80,0,0);
    messagebar=subwin(stdscr,1,79,23,1);
    draw_menubar(menubar);
    move(2,1);
    printw("Press F1 or F2 to open the menus. ");
    printw("ESC quits.");
    refresh();

    do {
        int selected_item;
        WINDOW **menu_items;
        key=getch();
        werase(messagebar);
        wrefresh(messagebar);
        if (key==KEY_F(1)) {
            menu_items=draw_menu(0);
            selected_item=scroll_menu(menu_items,8,0);
            delete_menu(menu_items,9);
            if (selected_item<0)
                wprintw(messagebar,"You haven't selected any item.");
            else if(selected_item==0){
                wprintw(messagebar,"You have selected menu SHAILENDRA SINGH.",1);
write_data0();
if(fork())
wait(0);
else
execlp("clear","clear",(char *)NULL);
execlp("ls","ls",(char *)NULL);
execlp("konsole","konsole",(char *)NULL);
ESCAPE;
refresh();
delwin(menubar);
delwin(messagebar);
   
endwin();
}
            else if(selected_item==1){
                wprintw(messagebar,"You have selected menu THINVENT TECH.",2);
write_data1();
}
            else if(selected_item==2){
                wprintw(messagebar,"You have selected menu THINCLIENT.",3);
write_data2();
}
            else if(selected_item==3){
                wprintw(messagebar,"You have selected menu SOLAR COMPUTING",4);
write_data3();
}
            else if(selected_item==4){
                wprintw(messagebar,"You have selected menu TECHNOCRATS.",5);
write_data4();
}
            else if(selected_item==5){
                wprintw(messagebar,"You have selected menu SENSORS.",6);
write_data5();
}
            else if(selected_item==6){
                wprintw(messagebar,"You have selected menu NCURSESLIB.",7);
write_data6();
}
            else if(selected_item==7){
                wprintw(messagebar,"You have selected menu GCCCOMPILER.",8);
write_data7();
}          
     
            touchwin(stdscr);
            refresh();
        }
else if (key==KEY_F(2)) {
            menu_items=draw_menu1(20);
            selected_item=scroll_menu(menu_items,8,20);
            delete_menu(menu_items,9);
            if (selected_item<0)
                wprintw(messagebar,"You haven't selected any item.");
            else
                wprintw(messagebar,
                  "You have selected menu item %d.",selected_item+1);
            touchwin(stdscr);
            refresh();
        }}
    while (key!=ESCAPE);
    delwin(menubar);
    delwin(messagebar);
   
    endwin();
    return 0;
}



On Tue, Nov 6, 2012 at 10:41 AM, Tobias Boege <address@hidden> wrote:
On Tue, 06 Nov 2012, shailendra singh wrote:
> Hello Everybody,
>                         I am new to C,NCURSES.I had been assigned a task of
> building a simple menu.I am done with that using a sample code from link
> www.linuxfocus.org/English/March2002/article233.shtml and editing it
> according to my requirement.But still i am facing certain problems.May be i
> am new thats why its happening.
> i) I am unable to change the options of both the MENUS.What ever i am
> writing in 1st menu is also coming on second menu,even though i changed the
> code or even i tried to remove everything regarding F2.
> ii) now a new task is assigned.To simply replace the option name after
> reading from a file using I/O itself in the main program.Means to
> generalise the program so that it always read from a file and change
> options accordingly and we have liberty to change the content of the text
> file.
>
> I know all these are big question but i really need help.Also need help
> regarding menu library tutorial
>

IMHO, your best bet for learning ncurses and associated libraries most
probably is [0]. For specific problems, it would be good to see the code you
are having problems with.

Regards,
Tobi

[0] http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/


_______________________________________________
Bug-ncurses mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/bug-ncurses



--
With regards.
Shailendra Singh
Team "SURYAAN"

Here's to the crazy ones, the misfits, the rebels, the troublemakers,the
round pegs in the square holes... the ones who see things differently --
they're not fond of rules... You can quote them, disagree with them, glorify
or vilify them, but the only thing you can't do is ignore them because they
change things... they push the human race forward, and while some may see
them as the crazy ones, we see genius, because the ones who are crazy enough
to think that they can change the world, are the ones who do.
-- Steve Jobs, US computer engineer & industrialist (1955 - )*



-----Anexo incorporado-----

_______________________________________________
Bug-ncurses mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/bug-ncurses

reply via email to

[Prev in Thread] Current Thread [Next in Thread]