Microsoft KB Archive/37414

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

Article ID: 37414

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q37414



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 &quot;Microsoft Visual Basic version 1.0 for MS-DOS Language Reference&quot;:

&quot;Avoid using I/O statements in a FUNCTION procedure called from an I/O statement; they can cause unpredictable results.&quot;

It is also related to the following restriction mentioned on Page 146 of the &quot;Microsoft Basic 7.0: Language Reference&quot; for versions 7.0 and 7.1: &quot;Avoid using I/O statements in a FUNCTION procedure called from an I/O statement; they can cause unpredictable results.&quot; See also Page 201 of &quot;Microsoft QuickBasic 4.0: Language Reference&quot; 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 &quot;\practice\test1.dat&quot; FOR INPUT AS #1 OPEN &quot;test2.dat&quot; 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 &quot;\practice\test1.dat&quot; FOR INPUT AS #1 OPEN &quot;test2.dat&quot; 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: KB37414

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.