1748139 Members
4085 Online
108758 Solutions
New Discussion юеВ

Header Files

 
SOLVED
Go to solution
Dave La Mar
Honored Contributor

Header Files

Seems we are missing some header files, i.e., iostream.h, istream.h, etc.
Q1. Are these only available with C++ and not the standard Ansi C (HP-UX 11.0)?
Q2. Since I am forced to use getch when reading an input file, I have run into a problem where control characters are not read in, i.e. ^A, etc.
Anyone have a workaround? Escape characters do not appear to work, i.e. \^A.

dl
"I'm not dumb. I just have a command of thoroughly useless information."
10 REPLIES 10
Umapathy S
Honored Contributor

Re: Header Files

Dave,
For question 1, yes. They are the header files for c++ library functions.

Umapathy
Arise Awake and Stop NOT till the goal is Reached!
A. Clay Stephenson
Acclaimed Contributor

Re: Header Files

I suspect that you are not setting curses up properly before calling getch(). You must, at the very least, call keypad() after doing an initscr(). getch() will then return, for example, the single value KEY_UP (0403) defined in curses.h no matter what the actual sequence generated by the up arrow key of your particular terminal.

The following code will loop until you press 'q':

-------------------------------------
#include

int cinitscr(void) /* returns TRUE if ok */
{
int h = FALSE;

(void) initscr();
h = (stdscr != NULL);
if (h)
{
(void) scrollok(stdscr,TRUE);
(void) cbreak();
(void) noecho();
(void) nl();
(void) keypad(stdscr,TRUE);
}
return(h);
} /* cinitscr */


int main()
{
if (cinitscr())
{
int ch = 0;

while (ch != 'q')
{
(void) printw("Enter char: ");
(void) refresh();
ch = getch();
if (ch >= ' ' && ch <= '~') (void) printw("%c",(char) ch);
else (void) printw(" ");
(void) printw(" %4o\n",ch);
(void) refresh();
}
endwin();
}
return(0);
}
---------------------------------------

compile/link like this:

cc key.c -lcurses -o key

If I got all that typed in without any boo-boos it should work just as claimed.

Oh, and no, ANSI C don't know nothing about them C++ constructs like streams.
If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: Header Files

Both great responses and much appreciated.

Clay, some additional information-
Purpose is to read in an array to the program that will be using this array to pass a buffer via a tcp socket program to a mainframe CICS application. Unfortunately, this buffer must be interpreted from the acii characters it receives in the buffer.
With that in mind, I don't believe the key characters would be viable, unless I am missing something.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor

Re: Header Files

Well, you are the one who started using getch() which only has meaning in a curses context but getch() when used as I have used it is actually perfect. If I understand what you are trying to do, when you see an up arrow key as input, for example, getch() will now not return "ESC[2" or whatever your terminal generates but will return KEY_UP. All you have to do is a reverse translation of KEY_UP into the character sequence that your client is expecting and there are terminfo functions that will do the reverse. This would let you have one TERM setting for input and another entirely different for output translation. This is actually a problem that was solved via curses decades ago because unlike PC's, UNIX applications had absolutely no clue as to what flavor terminal would be connected. In fact the same application (e.g. vi) is expected to behave identically independent of the terminal.
If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: Header Files

Clay-
Will give it a shot, report results, assign points. Thanks for clarification.
Incidentally, I would much rather have used getline() since the file I will read from has a number of parms I want included in the socket program. I was forced to use getch() due to the lack of appropriate header files.
This is not a life or death production issue.
The socket program is my emulation of scenarios where it performs as a banking institution, POS contoller, etc. and used for testing against the CICS socket programs.
Always appreciate your input and posts.
Best regards,
dl
"I'm not dumb. I just have a command of thoroughly useless information."
Gregory Fruth
Esteemed Contributor

Re: Header Files

There are many ways to read from an input
file. Why are you "forced to use getch"?
getch doesn't even read from a file, it reads
from the terminal! Perhaps you actually
want getc or getchar, or even scanf.

What is getline()? It does not appear
to be a part of the standard C library.
In C the usual way to read til EOL is
fgets().
Dave La Mar
Honored Contributor

Re: Header Files

I certainly led all in the wrong direction, been fiddling with this too long.
Gregory is correct.
Here is a snippet of my problem:

Below is how I read the input file-

output[i] = fgetc(input_file);

This is the data I want to read in-

\0^A0\0\0^D\0^N\0\0\0\0P\0 rrrrrrrrrrr00050000010005Y

This is what I get-

\00\0\0\0\0\0\0\0P\0 rrrrrrrrrrr00050000010005Y

As you can see, I lose the ^A, ^D, ^N.
If I hard code in the unix socket program, the buffer is passed correctly to the mainframe CICS socket program.
If I read the data into the unix socket program, the buffer passed to CICS is minus any ^A, etc.

Sorry for not stating this correctly in the beginning.

dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Header Files

Well, I don't know what you are using to output the data but let's bypass all the higher lvevel routines and use the read() system call itself to read in your input file. I can't speak for you output functions but I will absolutely positively assure you that this will load your input buffer:

----------------------------------
#include
#include
#include
#include

#define assign_errno(x) ((errno != 0) ? errno : (x))

static void problem(char *msg, int err)
{
(void) fprintf(stderr,"%s (%d)\n",msg,err);
(void) fflush(stderr);
return;
} /* problem */

#define STDOUT_FDES 1

int main(int argc, char *argv[])
{
int cc = 0;

if (argc > 0)
{
char *in_fname = NULL,s_err[512];
int in_fdes = -1;

in_fname = argv[1];
in_fdes = open(in_fname,O_RDONLY,0400);
if (in_fdes >= 0)
{
int n = 0;
unsigned char b[1024];

do
{
n = read(in_fdes,(void *) b,sizeof(b));
if (n > 0)
{
/* your stuff goes here */
/* send n bytes stored in b */
/* I'll just write on stdout */
(void) write(STDOUT_FDES,(void *) b,(size_t) n);
}
else
{
if (n < 0)
{
cc = assign_errno(-2);
problem("Read failed",cc);
}
}
}
while ((n == sizeof(b)) && (cc == 0));
(void) close(in_fdes);
}
else
{
cc = assign_errno(-1);
(void) sprintf(s_err,"Can't open file '%s'",in_fname);
problem(s_err,cc);
}
}
else
{
cc = 254;
problem("No input file",cc);
}
return(cc);
} /* main */

-----------------------------------------
Compile like this:
cc tester.c -o tester

As a test: tester tester will read in the source and output it to stdout, if I got all this typed in correctly.


That should do it. Where I'm doing the write() is where you will probably put your send() after setting up the socket.
If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: Header Files

Thanks Clay for not giving up on this.
Danged if that didn't work!!!!!!!

Thanks for all the time spent on this.

Best regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."