Binary I/O on stdin/stdout?

Rahul Dhesi dhesi at bsu-cs.UUCP
Sun Apr 3 04:50:44 AEST 1988


In article <3295 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) writes:
[about VMS stream-LF files]
>The fact that it's called "stream-LF" (as distinct from "stream-CR" or just
>"stream") suggests that the newlines which terminate the records have some
>significance to the OS.  Is it legal, for example, to write 70000 characters
>without a newline?  If not, this doesn't seem like an acceptable format for
>binary mode.

Yes, a VMS C program can write any number of characters before writing
a newline, and it works, and it can all be read back.  VMS does handle
records of arbitrary length in stream-LF files, SO LONG AS ALL ACCESS
IS THROUGH THE VMS C LIBRARY.  Things break down, however, when such a
file is manipulated in any other manner, as is shown by the following
program.

-----cut here-----
#include <stdio.h>
/* testing VMS stream-LF format */
#define HOWMUCH 2000     /* how much to write as a single record */
#define FNAME "junk.dat" /* what to call the file */

main()
{
   FILE *f; int i; int c;

   f = fopen (FNAME, "w");
   if (f == NULL) 
      perror("write");
   for (i = 0;  i < HOWMUCH;  i++)
      fputc ('x', f);
   fputc ('\n', f);            /* terminates stream-LF record *whew* */
   fclose (f);

   /* now read file and dump to screen */
   f = fopen (FNAME, "r");
   if (f == NULL)
      perror ("read");
   for (i = 0; i < HOWMUCH + 1; i++) {
      c = fgetc (f);
      fputc (c, stdout);
   }
}
-----cut here-----

This program writes a 2000-character record to junk.dat, then reads
junk.dat and sends it to the screen.  I ran it both interactively and
under batch.

(a) Interactively:  What is printed on the screen by the program is
what I expected:  2000 'x' characters.  But when I gave the VMS command
"type junk.dat" I got a QIO error, presumably because the VMS "type"
command can't handle 2000-character records.

(b) Under batch:  The output from the program as shown in the batch log
was not 2000 'x' characters followed by a newline.  Instead, I saw
lines of 512 'x' characters.  Clearly, a newline was being forced after
512 characters at most.  When the batch file did "type junk.dat", a QIO
error occurred again.
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!dhesi



More information about the Comp.lang.c mailing list