mvprintw() 只打印部分

mvprintw() only printing partially

我刚刚迁移到 Ubuntu,我正在尝试适应 curses 和 ncurses。我正在尝试构建一个使用箭头导航、使用 Q 退出和 F2 帮助的简单程序。问题是,在显示帮助文本后,当它返回到初始菜单时,它只打印 mvprintw 的初始部分。这是我的代码:

代码已更新

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <locale.h>
#include <ncurses.h>

#define MIN 1
#define MAX 6

//STRUCT PARA ARMEZAR DADOS PARA CADASTRO
typedef struct{
    char nome[50];
    long int rg;
    char cpf[11];
    unsigned short idade;
    float alt;
}REGISTRO;

REGISTRO dados;

//Inicialização da função cadastro
void cadastro(REGISTRO *dados);

int main(){
    short c, opc, x, y;

    char path[] = "data/";
    char pathA[] = "data/dados.bin";

    setlocale(LC_ALL, "");
    initscr();
    raw();
    noecho();
    keypad(stdscr, TRUE);

    FILE *data;

    if(!mkdir(path, 0777)){
        printf("Pasta \"data\" nao encontrada.\nCriando diretório...\n");
    }

    if((data = fopen(pathA, "r+"))==NULL){
        printf("Não foi possivel encontrar o arquivo dados.bin!\nUm novo arquivo será criado...\n");
        if((data = fopen(pathA, "w+"))==NULL){
            printf("Não foi possivel criar o arquivo.\nO programa será finalizado em breve!\n");
            exit(1);
        }
    }

    do{
        erase();
        mvprintw(0,0,"-----DATABASE EVIL CORP-----\n");
        printw("1. Cadastro\n2. Whatever\n3. Whatever\n4. Whatever\n5. Whatever\n6. Whatever\n");
        x = 0; y = 1; opc = 1;
        refresh();
        do{
            mvprintw(7,0,"OPC: %hd", opc);
            mvprintw(8,0,"C: %hd", c);
            move(y,x);
            c = getch();

            if(c == 'Q' || c == 'q'){
                opc=0;
                c=0;
            }

            if(c == KEY_F(2)){
                erase();
                mvprintw(0, 0, "MENU DE AJUDA");
                x = 0, y = 1, opc = 0;
                refresh();
                getch();
            }

            if(c == '\n'){
                switch(opc){
                    case 1:
                        erase();
                        cadastro(&dados);
                        getch();
                        opc=0;
                    break;

                    case 2:
                        erase();
                        printw("TESTE");
                        getch();
                        opc=0;
                    break;

                    default:
                        erase();
                        mvprintw(0,0, "Opcao invalida ou ainda em construcao.");
                        getch();
                        opc=0;
                    break;
                }
            }

            switch(c){
                case KEY_UP:
                    if(opc>1){
                        opc--;
                        y--;
                    }

                    else{
                        opc=MAX;
                        y=MAX;
                    }
                break;

                case KEY_DOWN:
                    if(opc<6){
                        opc++;
                        y++;
                    }
                    else{
                        opc=MIN;
                        y=MIN;
                    }
            }

        }while(opc);
    }while(c);

    endwin();
    return 0;
}

void cadastro(REGISTRO *dados){
    erase();
    mvprintw(0, 0, "-----REGISTRO DE DADOS-----\nNome: ");
    refresh();
    fgets(dados->nome, sizeof(dados->nome), stdin);
    printw("RG: ");
    scanf("%ld", &dados->rg);
    fflush(stdin);
    printw("CPF: ");
    fgets(dados->cpf, sizeof(dados->cpf), stdin);
    printw("Altura: ");
    scanf("%f", &dados->alt);
    printw("Idade: ");
    scanf("%hd", &dados->idade);
}

已编辑:新问题,当我进入 cadastro() 函数时,我看不到我在输入什么,但不知何故我知道有什么事情发生了,按下随机键后我可以回到第一个菜单,一切恢复正常。我认为这与ncurses屏幕有关,但我不确定。

问题是程序运行 shell 进程 "clear" 而没有执行 endwin to tell ncurses that it is leaving screen mode temporarily. Use the erase() 函数。

如果您将 system("clear") 替换为 erase(),它将按照您的预期运行。