Using STDIN in BINARY mode... 
Author Message
 Using STDIN in BINARY mode...


Quote:

>I'm using the BORLAND C++ (Version 4.0) compiler under MS-DOS.
>Does anyone know whether there are restrictions about using
>standard streams in BINARY mode?

The restriction is that these streams (stdin, stdout and stderr) are created
for you before your program starts, and the C language defines them as text
streams.  There is nothing you can do to turn them into binary streams.

Oh, and a void return from main is undefined behavior.

Quote:
>If yes, are these restrictions typical for MS-DOS?

>The compilation of the following source is o.k.

>#include <stdio.h>
>#include <stdlib.h>

>void main()

To properly interface with the environment, a C program must return int
from the main function.

Quote:
>{
> char *buffer = malloc(81);
> int   i = 0;
> FILE *ptr;

> printf("Console read in TEXT mode.\n");
> printf("Enter (up to) 80 characters:\n");
> while ( (buffer[i++] = getchar()) != EOF )
>         ;  /* null statement */
> buffer[i] = '\0';
> printf("Your input was\n%s\n", buffer);

> printf("Console read in BINARY mode.\n");
> ptr = freopen("CON", "rb", stdin);
> if ( ptr == NULL ) { perror("freopen failed"); abort; }

Abort is not a command, but a function. Invoking it without an argument
list means that a pointer to the function is obtained, which is then
discarded.

Quote:
> if ( ptr == stdin )  printf("freopen successful\n");

This test is unnecessary. If ptr is anything other than null, you
have succeeded.

Quote:
> printf("Enter (up to) 80 characters:\n");
> while ( (buffer[i++] = getchar()) != EOF )
>         ;  /* null statement */
> buffer[i] = '\0';

You have not reset the value of i to zero, so you don't really have 80
characters of space remaining.

[ snip ]

Quote:
>out: freopen successful
>out: Enter (up to) 80 characters:

>The cursor is placed at the beginning of the next line after
>the latter message, but any input is prevented, and the program
>is halted.
>(I have to reboot MS-DOS.)

Shrug. Ask in comp.os.msdos.programmer. The C language doesn't define
that a file "CON" exists which is identified with console input
or output.


Fri, 29 Oct 1999 03:00:00 GMT  
 Using STDIN in BINARY mode...

BTW: You should ask this question in a MS-DOS specific newsgroup.


Quote:
(Michel Castelein) writes:
>[...]
>The cursor is placed at the beginning of the next line after
>the latter message, but any input is prevented, and the program
>is halted.

The problem is: How can end of file be signalled?

In MS-DOS, if reading from a file, end of file is signalled
by the read system call returning null bytes read.

If reading from a terminal device, the read sys. call
blocks until characters are input.  There is no way to have
the read sys. call return with 0 bytes read.

Therefore, the run-time lib. takes a control-z character in
the line of input as "this means end of    file".     If you open
the terminal device in binary mode, this special meaning of
control-z is lost, and there is no way to signal end of
input.

Your program turns out to read input forever, overflowing
the buffer, if the number of characters read so far exceeds
79.

Try the following:

binread.c:

#include <ctype.h> /* isgraph()   */
#include <stdlib.h> /* strtoul(), NULL,   EXIT_..., perror() */
#include <stdio.h>

int main(int argc, char **argv)
{
   /* try to reopen stdin
   */
   If (NULL == freopen("CON","rb",stdin))
   {
      perror("could not    reopen stdin");
      exit(EXIT_FAILURE);
   }
   else
      printf("stdin successfully reopened\n");

   /* if 2nd cmd line arg. provided, set buffering:
         0  -->      unbuffered
         >0 -->   fully buffered with buffersize provided
      else use default buffering mode (fully buff. with BUFSIZ)
   */
   if (argc >= 3)
   {
      size_t buffersize = strtoul(argv[2],(char **)NULL,0);

      if (buffersize == 0)
         setbuf(stdin,(char *)NULL); /* unbuffered */
      else
         if (setvbuf(stdin,(char *)NULL,_IOFBF,buffersize))
         {
            perror("set    buffering mode");
            exit(EXIT_FAILURE);
         }
   }
   {
      size_t readCount;

      for (
         readCount = argc >= 2?      strtoul(argv[1],(char **)NULL,0): 0;
         readCount > 0 && !(feof(stdin)      || ferror(stdin));
         --readCount
      )
      {
         printf("%lu ==>",(unsigned long)readCount); fflush(stdout);
         {
            int c = getchar();

            if (c == EOF)
               printf("<EOF>");
            else if (isgraph(c))
               putchar(c);
            else
               printf("<%d>",c);
         }
         putchar('\n');
      }
   }

   return EXIT_SUCCESS;

Quote:
}

========= end of binread.c =========

invoke it:

binread no-of-characters-to-read optional-buffer-size

binread 7
   read 7 characters, input buffering mode = default (fully buff.?)

binread 9 4
   read 9 characters, inp. buff. mode = fully buff. with buf. size = 4

binread 20 0
   read 20 characters, inp. buff. mode = unbuffered

Observe, how many characters you must input, before the first of the
junk is echoed.

Regards, Friedhelm.



Fri, 29 Oct 1999 03:00:00 GMT  
 Using STDIN in BINARY mode...

Quote:

[snip]
> >I'm using the BORLAND C++ (Version 4.0) compiler under MS-DOS.
> >Does anyone know whether there are restrictions about using
> >standard streams in BINARY mode?

> The restriction is that these streams (stdin, stdout and stderr) are created
> for you before your program starts, and the C language defines them as text
> streams.  There is nothing you can do to turn them into binary streams.

Ain't necessarily so.

The following works under Borland v4.52 as a means to set
the stdin/stdout modes to binary.

/* change the mode for input/output streams to binary */

  if( fInp == stdin )
    setmode( 0,O_BINARY );

  if( fOut == stdout )
    setmode( 1,O_BINARY );

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys



Fri, 29 Oct 1999 03:00:00 GMT  
 Using STDIN in BINARY mode...

I'm using the BORLAND C++ (Version 4.0) compiler under MS-DOS.
Does anyone know whether there are restrictions about using
standard streams in BINARY mode?
If yes, are these restrictions typical for MS-DOS?

The compilation of the following source is o.k.

#include <stdio.h>
#include <stdlib.h>

void main()
{
 char *buffer = malloc(81);
 int   i = 0;
 FILE *ptr;

 printf("Console read in TEXT mode.\n");
 printf("Enter (up to) 80 characters:\n");
 while ( (buffer[i++] = getchar()) != EOF )
         ;  /* null statement */
 buffer[i] = '\0';
 printf("Your input was\n%s\n", buffer);

 printf("Console read in BINARY mode.\n");
 ptr = freopen("CON", "rb", stdin);
 if ( ptr == NULL ) { perror("freopen failed"); abort; }
 if ( ptr == stdin )  printf("freopen successful\n");
 printf("Enter (up to) 80 characters:\n");
 while ( (buffer[i++] = getchar()) != EOF )
         ;  /* null statement */
 buffer[i] = '\0';
 printf("Your input was\n%s\n", buffer);

Quote:
}

This is the console logging when executing the program under
MS-DOS 6.20:

out: Console read in TEXT mode.
out: Enter (up to) 80 characters:
in : a first line[Enter]
in : a second line[Ctrl-Z][Enter]
out: Your output was
out: a first line
out: a second line
out: Console read in BINARY mode.
out: freopen successful
out: Enter (up to) 80 characters:

The cursor is placed at the beginning of the next line after
the latter message, but any input is prevented, and the program
is halted.
(I have to reboot MS-DOS.)

TIA.

Michel

Michel Castelein,
OS/390 (MVS) System Engineer & Education Consultant
---------------------------------------------------

Home page: http://www.*-*-*.com/ ~pub00543/
company:   JEEVES CONSULTING N.V.
           Mechelses{*filter*}weg 277,
           B - 1800 Vilvoorde (Belgium)
           phone +32-2-2516650 / fax +32-2-2525755



Sat, 30 Oct 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Binary and Text Modes and Stdin/Stdout

2. MSC6.0 - Binary Stdin Mode

3. Closing/reopening stdin in binary mode w/ Turbo C 2.0

4. Binary mode stdin

5. flushing stdin after using fgets( foo, 100, stdin )

6. Text mode or binary mode ?

7. ofstream binary mode not binary?

8. Read binary data from stdin

9. Reading binary data from stdin on NT.

10. stdin binary

11. Set stdin/stdout in binary on UNIX

12. binary data from stdin on NT

 

 
Powered by phpBB® Forum Software