![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0
![]() |
I am trying to print the contents of a file to a curses window. I have a main program that takes each line of a file and places it into an array of strings. I then pass that array to the following function, along with its number of items:
int showCurses(char *lineList[], int items)
{
int j;
initscr();
noecho();
getmaxyx(stdscr, row, col);
for(j = 0; j < items; j++)
{
printw(lineList[j]);
}
refresh();
while(getch() != 'e'){}
endwin();
return 0;
}So, what am I doing wrong? I have much more advanced things to do down the road, but I don't evne know how to get the file contents displayed! (In the future I'll have to be able to scroll up and down through the file.) |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
How are you calling the function?
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0
![]() |
showCurses(fileBuffer, i);, where: char *fileBuffer[1000] and int i. Within the showCurses function, I have checked the lineList for accurate data, which it shows when printed, and the same with items.
It shows me a blank screen and then, as expected, waits for me to press 'e' to exit the curses window. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
I'm guessing that your lines don't contain end of line markers, so all of the printw() calls will write to the first line. Try changing the call to printw() in the loop to "printw("%s\n", lineList[j]);" or add a second line "printw("\n");" in the loop.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0
![]() |
After adding the '\n' character to each line, I am still seeing the same output (sometimes a blank screen, sometimes a few random lines throughout the screen, depending on the file read). Since nobody is seeing an error immediately, perhaps someone could compile it and see what its doing for them?
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
We would also need the code that you call the function with. Are your strings zero terminated?
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0
![]() |
Here is my entire program. My strings, I am assuming, are zero terminated since I did a malloc(strlen(str2) + 1) and then strcpy(str1, str2).
#include <stdio.h>
#include <string.h> /* for strlen, strcpy */
#include <stdlib.h> /* for malloc */
#include <ncurses.h>
#define NUM_STRINGS 1000
FILE *Fp;
int row, col;
int main (int argc, char *argv[])
{
char *fileBuffer[NUM_STRINGS]; /* array of pointers */
char tempString[BUFSIZ]; /* size of input buffer defined in stdio.h */
int i = 0; /* line counter */
printf("argc = %d\n", argc);
if (argc < 2)
{
printf("There was no file name on the command line\n");
return 0;
}
if((Fp = fopen(argv[argc - 1], "r")) == NULL)
{
printf("Couldn't open file %s for reading\n", argv[argc-1]);
return 0;
}
printf("Opened file %s for reading\n", argv[argc-1]);
while((i < NUM_STRINGS) && fgets(tempString, sizeof(tempString), Fp))
{
fileBuffer[i] = malloc(strlen(tempString) + 1);
if (!fileBuffer[i])
{
printf("Can't allocate %d bytes for fileBuffer[%d]\n",
strlen(tempString)+1, i);
break;
}
strcpy(fileBuffer[i], tempString);
// printf("%d: %s", i, fileBuffer[i]);
i++;
}
showCurses(fileBuffer, i);
return 0;
}
int showCurses(char *lineList[], int items)
{
int j;
initscr();
noecho();
getmaxyx(stdscr, row, col);
for(j = 0; j < items; j++)
{
printw("%s\n", lineList[j]);
}
refresh();
while(getch() != 'e'){}
endwin();
return 0;
} |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Works for me except for the last line.
tested with: hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line nnnnn output: hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hello this is just a test a few lines of text delimeted by new line hellothis is just a testa few lines of textdelimeted by new linehellothis is just a testa few ln
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0
![]() |
It must have to do with the fact that I'm using Cygwin then...what are you using?
And did you compile it like this: gcc file.c -lcurses -o -file |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
I'm on Linux using ncurses. I compiled it with:
gcc -Wall -lncurses -ofile file.c
__________________
PFO - My daily dose of technology. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|