HP3000-L Archives

March 2006, Week 3

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"Miller, Keven" <[log in to unmask]>
Reply To:
Miller, Keven
Date:
Tue, 21 Mar 2006 17:50:06 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (61 lines)
john pitman wrote:
> Does anybody have any C samples handy that can do this please? I don't want
> to go to intrinsics for portability reasons...initial attempts with getc()
> don't seem to work for us.

Things to remember about C:
file streams (FILE *) are usually buffered files. Such that reads will
fill a buffer, but only return to user code that which you ask. There
are 3 choices in buffering, Full, Line, and None
(_IOFBF, _IOLBF, _IONBF).
The default for stdin is Line buffering; so you need to hit return to
complete the io of filling the buffer.

Below in the sample, I changed stdin to be No buffering. But the effect
is not what you want. It will input 1 char, and issue a newline. Then
input 1 char again -- up to the number of bytes specified.

The other sample uses "read". This appears to function as I think
you want. The functions: open, read, write, and close, I thought
were defined in stdio or stdlib.h, I had learned that they were
part of the C library.
But now, their definition is only found in unistd.h, and more
of a posix thing.

Note 1: you can set the buffering for a stream file before
any IO is done. Once done and the buffers are allocated, you
cannot change it (well, so says the documentation :) ).

Note 2: I believe you can use "read" as below while maintaining
stdin as line buffered. Allowing you both methods for input.

Keven Miller

  #pragma list off
  #include <stdio.h>

  int main ( int ac, char *av [] )
  {
     int   len;
     char  buf [256];

     memset ( buf, 0, sizeof (buf));
     printf ( "Prompt>" );
     fflush ( stdout);

     len = read ( fileno (stdin), buf, 3 );
     printf ( "Read (%d) [%s]\n", len, buf );

     memset ( buf, 0, sizeof (buf));
     setvbuf ( stdin, NULL, _IONBF, 0 );
     printf ( "Prompt>" );
     fflush ( stdout);

     fgets ( buf, 3+1, stdin );
     len = strlen ( buf );
     printf ( "fgets (%d) [%s]\n", len, buf );
  }

* To join/leave the list, search archives, change list settings, *
* etc., please visit http://raven.utc.edu/archives/hp3000-l.html *

ATOM RSS1 RSS2