gpt4 book ai didi

c - 如何将 mvprintw 与指向字符串的指针数组一起使用

转载 作者:行者123 更新时间:2023-11-30 16:10:09 24 4
gpt4 key购买 nike

我正在尝试使用 mvprintw 从数组中打印单词。然而,无论我尝试什么, mvprintw 似乎都不起作用。



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


#define MAX_WORD_LENGTH 12 /* The maximum length a word can be. */
#define MAX_WORDS 75 /* The maximum words that can be in the DS4Talker. */


void trim_whitespace(char *string);

int read_words(char *word_list[MAX_WORDS], char *file_name);


// Compile with gcc lab09.c -o lab09 -lncurses
// Run with ./ds4rd.exe -d 054c:05c4 -D DS4_BT -t -b -bt -bd | ./lab09 word_list.txt


/*----------------------------------------------------------------------------
- Implementation -
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
char *word_list[MAX_WORDS];
int word_count = read_words(word_list, argv[1]);
int i = 0; int row = 1; int col = 1;
initscr();


for(int c = 0; c < word_count; c++){
mvprintw(col, row, "%15s\n", word_list[c]);
if(c % 15 == 0) {
row++;
col++;
}
printf("This is doing nothing :D\n");
}

//fclose(fptr);
refresh();
endwin();


return 0;
}


* @param string - The string to ensure has no whitespace.
*/
void trim_whitespace(char *string)
{
int length = strlen(string);
if (length == 0) return;

int i = length - 1;
while (isspace(string[i]) && i >= 0)
{
string[i] = '\0';
i--;
}
}


* @param word_list - The list of words where the words taken from the given
* file will be placed.
* @param file_name - The name of the file to read from.
* @return - The number of words read from the file.
*/
int read_words(char *word_list[MAX_WORDS], char *file_name)
{
int number_of_words_read = 0;
char line[MAX_WORD_LENGTH];
char *pointer;
FILE *file = fopen(file_name, "r");

while (!feof(file))
{
pointer = fgets(line, MAX_WORD_LENGTH, file);
if (pointer != NULL)
{
trim_whitespace(line);
word_list[number_of_words_read] = (char *) malloc(strlen(line) + 1);
strcpy(word_list[number_of_words_read], line);
number_of_words_read++;
}
}

fclose(file);
return number_of_words_read;
}

我将附上我的 cygwin 终端的屏幕截图,告诉我 mvprintw 已被隐式声明。不确定如何解决这个问题,但也许这是问题的一部分。如果此屏幕截图味道不好,请告诉我,请不要投反对票。

enter image description here

最佳答案

我认为您只是忘记包含所需的库

#include <curses.h>

通常,当您收到“隐式函数声明”警告时,这意味着您可能忘记包含一些库。

阅读一些有关 mvprintw() 的文档, endwin()

如果我错了,请原谅,这是我第一次尝试在这里提供帮助:)

关于c - 如何将 mvprintw 与指向字符串的指针数组一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com