0

Im trying to use mvprintw to print out words from an array. However no matter what I try mvprintw seems to be non functional.



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

Im going to include a screenshot of my cygwin terminal telling me that mvprintw has been implicitly declared. Not sure how to fix this, but perhaps its part of the issue. If this screenshot is in bad taste, tell me, please do not downvote.

enter image description here

  • 1
    In addition to the answer below, you're resetting the output to the same position for every word (thus overwriting the last word each time), except that every 15th word, you increment both the row and the column by a single position. This can't be what you intended. – William McBrine Nov 22 '19 at 07:38

1 Answers1

0

I think you just forgot to include needed library

#include <curses.h>

usually, when you have an "implicit declaration of function" warning it means that you probably forgot to include some libraries.

read some documentation about mvprintw(), endwin()

excuse me if I'm wrong, it's my first try to help here :)

Sinking
  • 95
  • 8