Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 15th, 2006, 9:27 PM   #1
smoothdogg00
Newbie
 
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0 smoothdogg00 is on a distinguished road
Question Help with curses! (Beginner)

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;
}
I know that the value of items is correct and that the array contains the contents of the file with each line being a string within the array.

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.)
smoothdogg00 is offline   Reply With Quote
Old Apr 15th, 2006, 9:37 PM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
How are you calling the function?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 15th, 2006, 9:45 PM   #3
smoothdogg00
Newbie
 
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0 smoothdogg00 is on a distinguished road
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.
smoothdogg00 is offline   Reply With Quote
Old Apr 15th, 2006, 9:58 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Apr 15th, 2006, 10:02 PM   #5
smoothdogg00
Newbie
 
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0 smoothdogg00 is on a distinguished road
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?
smoothdogg00 is offline   Reply With Quote
Old Apr 15th, 2006, 10:29 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
We would also need the code that you call the function with. Are your strings zero terminated?
grumpy is offline   Reply With Quote
Old Apr 15th, 2006, 10:34 PM   #7
smoothdogg00
Newbie
 
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0 smoothdogg00 is on a distinguished road
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;
}
smoothdogg00 is offline   Reply With Quote
Old Apr 16th, 2006, 12:04 AM   #8
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Apr 16th, 2006, 12:31 AM   #9
smoothdogg00
Newbie
 
Join Date: Mar 2006
Location: Maine
Posts: 11
Rep Power: 0 smoothdogg00 is on a distinguished road
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
smoothdogg00 is offline   Reply With Quote
Old Apr 16th, 2006, 1:24 AM   #10
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
I'm on Linux using ncurses. I compiled it with:
gcc -Wall -lncurses -ofile file.c
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC