Cannot Nest I/O Statements or Functions in I/O Statements

ID: Q37414


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
  • Microsoft BASIC Compiler for MS-DOS and MS OS/2, versions 6.0, 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, versions 7.0, 7.1


SUMMARY

With two sequential files open, #1 for INPUT and #2 for OUTPUT, the following statement incorrectly sends output to the screen instead of to file #2:


   PRINT #2, INPUT$(10, #1) 
This behavior occurs in the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0; in Microsoft QuickBasic for MS-DOS, versions 4.0, 4.0b, and 4.5; in Microsoft Basic Compiler for MS-DOS and MS OS/2, versions 6.0 and 6.0b; and in Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, versions 7.0 and 7.1. This limitation is documented in the README.DOC file for Microsoft Basic PDS for MS-DOS and MS OS/2, versions 7.0 and 7.1.

This information is also included with the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0.

To work around this behavior, put the INPUT$ function into a temporary string variable, then PRINT that temporary string into the second file.

The general rule to observe is as follows: do not nest input/output (i/o) statements or functions within other i/o statements or functions. This is a design limitation.


MORE INFORMATION

The above limitation is related to the following restriction mentioned on Page 149 of the "Microsoft Visual Basic version 1.0 for MS-DOS Language Reference":

"Avoid using I/O statements in a FUNCTION procedure called from an I/O statement; they can cause unpredictable results."
It is also related to the following restriction mentioned on Page 146 of the "Microsoft Basic 7.0: Language Reference" for versions 7.0 and 7.1: "Avoid using I/O statements in a FUNCTION procedure called from an I/O statement; they can cause unpredictable results." See also Page 201 of "Microsoft QuickBasic 4.0: Language Reference" for versions 4.0 and 4.0b for the same caveat.

The following code example shows the unexpected behavior:

' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.
'
' This incorrectly writes to the screen.
OPEN "\practice\test1.dat" FOR INPUT AS #1
OPEN "test2.dat" FOR OUTPUT AS #2
PRINT #2, INPUT$(10, #1) 
The input file TEST1.DAT is as follows:
123456789012345
The following program shows how to work around this problem by using a temporary string variable to accept the input before writing to file #2. This program correctly writes to file #2, not the screen:

' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.

OPEN "\practice\test1.dat" FOR INPUT AS #1
OPEN "test2.dat" FOR OUTPUT AS #2
CopyString$ = INPUT$(10, #1)
PRINT #2, CopyString$ 
Microsoft QuickBasic for MS-DOS, version 3.0 successfully writes to file #2, not the screen. Microsoft GW-Basic for MS-DOS, version 3.22 also writes to file #2, not the screen (when you put line numbers in the source file).

Additional query words: VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords :
Version : MS-DOS:1.0,4.0,4.0b,4.5; :6.0,6.0b,7.0,7.1
Platform : MS-DOS
Issue type :


Last Reviewed: December 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.