Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 课程';printw()不';行不通_C_Ncurses - Fatal编程技术网

C 课程';printw()不';行不通

C 课程';printw()不';行不通,c,ncurses,C,Ncurses,我用文件中的字符串填充了一个矩阵,printf()可以正确地看到,但是printw()似乎与代码的其余部分不一致。它适用于普通字符串,但对于来自该矩阵的字符串,它不起作用 #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <ncurses.h> int main (int argc, char const *a

我用文件中的字符串填充了一个矩阵,printf()可以正确地看到,但是printw()似乎与代码的其余部分不一致。它适用于普通字符串,但对于来自该矩阵的字符串,它不起作用

#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;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char const*argv[]
{
char**matrice=malloc(sizeof(char*)*51);
大小n字节;
int i=0,j=0;
文件*lab=fopen(argv[1],“r”);
while((getline(&matrice[i],&nbytes,lab)!=-1))
{
i++;
}
printf(“%s”,矩阵[0]);
getchar();
initscr();/*开始诅咒模式*/
cbreak();/*线路缓冲已禁用*/
键盘(stdscr,TRUE);/*我们得到F1、F2等*/
noecho();/*在我们做getch时不要回显()*/
printw(矩阵[0]);
printw(“虚拟线”\n);
刷新();
getch();
endwin();
返回退出成功;
}

这与
printw()无关。
,您只是没有正确分配内存。在这里:

char** matrice = malloc(sizeof(char*)*51);
您不会为实际字符串分配任何内存。您为51个指针分配内存,但不为它们分配任何内存。因此,您的
getline()
调用试图读取未分配的内存,这会产生未定义的行为。在你的程序正常运行之前,所有的赌注都是无效的

您需要为这51个指针中的每个指针分配一些内存,或者只使用一个静态数组

如前所述,您也无法在最后
free()
内存
malloc()
,并且无法检查
malloc()
中的返回值以检查它是否实际提供了内存

这就是你想要的:

#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;
}

但是分配您自己的内存并使用
fgets()
更好,如果有一种非常好的标准方法,那么就不需要使用非标准扩展,即使您首先使用的是第三方库,如ncurses。

@settmbrenero:您的程序有未定义的行为,所以任何事情都可能发生。没有必要试图理解为什么,它只是坏了,你需要修复它。您的
printf()
调用似乎可以工作,这一事实是侥幸的。当我试着运行你的原始程序时,我的系统只是出现了故障。不为您分配,但您必须从初始化为null的指针和初始化为0的大小开始。@WumpusQ.Wumbley:是的,我要指出,
getline()
也是POSIX扩展。
#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;
}