简体   繁体   English

ncurses的printw()不起作用

[英]ncurses' printw() doesn't work

i filled a matrix with strings from a file, that printf() see correctly, but printw() doesn't seem to agree with the rest of the code. 我用文件中的字符串填充矩阵,printf()看得正确,但printw()似乎不同意其余的代码。 it works with normal strings, but with strings from that matrix, it doesn't work. 它适用于普通字符串,但是使用该矩阵中的字符串,它不起作用。

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ncurses.h>

int main (int argc, char const *argv[])
{
    char** matrice = malloc(sizeof(char*)*51);
    size_t nbytes;
    int i = 0, j = 0;

    FILE* lab = fopen(argv[1], "r");

    while((getline(&matrice[i], &nbytes, lab) != -1))
    {
        i++;
    }
    printf("%s", matrice[0]);
    getchar();
    initscr();          /* Start curses mode        */
    cbreak();           /* Line buffering disabled  */
    keypad(stdscr, TRUE);       /* We get F1, F2 etc..      */
    noecho();           /* Don't echo() while we do getch */
    printw(matrice[0]);
    printw("dummy line"\n);
    refresh();
    getch();
    endwin();
    return EXIT_SUCCESS;
}

This is nothing to do with printw() , you're just not allocating memory correctly. 这与printw()无关,你只是没有正确分配内存。 Here: 这里:

char** matrice = malloc(sizeof(char*)*51);

you don't allocate any memory for your actual strings. 你没有为你的实际字符串分配任何内存。 You allocate memory for 51 pointers, but you don't allocate any memory for them to point to. 您为51个指针分配内存,但是您没有为它们分配任何内存以指向它们。 Your getline() call is therefore trying to read into unallocated memory, which yields undefined behavior. 因此,您的getline()调用将尝试读入未分配的内存,从而产生未定义的行为。 All bets are off until you get your program behaving properly. 所有投注都会关闭,直到您的程序正常运行。

You'll need to allocate some memory for each of those 51 pointers, or just use a static array. 你需要为这51个指针中的每一个分配一些内存,或者只使用一个静态数组。

As written, you also fail to free() the memory you malloc() at the end, and you fail to check the return value from malloc() to check if it actually gave you the memory. 如上所述,您也无法free()malloc()内存,并且您无法检查malloc()的返回值以检查它是否实际为您提供了内存。

Something like this is what you want: 这样的东西就是你想要的:

#include <stdio.h>
#include <stdlib.h>

#define ARRSIZE 51
#define STRSIZE 100

int main(void) {
    int i;

    char ** matrice = malloc(ARRSIZE * sizeof(*matrice));
    if ( matrice == NULL ) {
        fputs("Couldn't allocate memory!", stderr);
        return EXIT_FAILURE;
    }

    for ( i = 0; i < ARRSIZE; ++i ) {
        matrice[i] = malloc(STRSIZE);
        if ( matrice[i] == NULL ) {
            fputs("Couldn't allocate memory!", stderr);
            return EXIT_FAILURE;
        }
    }

    /* Rest of your program */

    for ( i = 0; i < ARRSIZE; ++i ) {
        free(matrice[i]);
    }
    free(matrice);

    return 0;
}

EDIT: If you really want to use getline() , here's a version of your original program that'll work: 编辑:如果你真的想使用getline() ,这是你的原始程序的一个版本,它将起作用:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ncurses.h>

int main(int argc, char const *argv[]) {
    size_t nbytes = 0;
    int i = 0, j = 0;

    if ( argc < 2 ) {
        fputs("You must specify a file name!", stderr);
        return EXIT_FAILURE;
    }

    FILE *lab = fopen(argv[1], "r");
    if ( lab == NULL ) {
        fputs("Couldn't open file!", stderr);
        return EXIT_FAILURE;
    }

    char **matrice = malloc(sizeof(char *) * 51);
    if ( matrice == NULL ) {
        fputs("Couldn't allocate memory!", stderr);
        return EXIT_FAILURE;
    }

    for ( j = 0; j < 51; ++j ) {
        matrice[j] = NULL;
    }

    while ( i < 50 &&
            (getline(&matrice[i], &nbytes, lab) != -1) ) {
        i++;
    }

    if ( i == 0 ) {
        fputs("File was empty.", stderr);
        free(matrice[0]);
        free(matrice);

        return EXIT_FAILURE;
    }

    printf("%s", matrice[0]);
    getchar();
    initscr();                  /* Start curses mode        */
    cbreak();                   /* Line buffering disabled  */
    keypad(stdscr, TRUE);       /* We get F1, F2 etc..      */
    noecho();                   /* Don't echo() while we do getch */
    printw(matrice[0]);
    printw("dummy line\n");
    refresh();
    getch();
    endwin();

    for ( j = 0; j <= i; ++j ) {
        free(matrice[j]);
    }
    free(matrice);

    return EXIT_SUCCESS;
}

but allocating your own memory and using fgets() is better, there's no call for using non-standard extensions when there's a perfectly good standard way of doing it, even if you're using a third-party library like ncurses to begin with. 但是分配你自己的内存并使用fgets()更好,当有一个非常好的标准方法时,即使你正在使用像ncurses这样的第三方库,也不需要使用非标准扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM