vbdpss.hlp (Table of Contents; Topic list)
Article Q37414
                                                 Contents  Index  Back
─────────────────────────────────────────────────────────────────────────────
                           Knowledge Base Contents  Knowledge Base Index
 
 Cannot Nest I/O Statements or Functions in I/O Statements - Q37414
 
 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)
 
 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."
 
 The following code example shows the unexpected behavior:
 
 Code Example
 ------------
 
 ' 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 "ractice\est1.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 "ractice\est1.dat" FOR INPUT AS #1
 OPEN "test2.dat" FOR OUTPUT AS #2
 CopyString$ = INPUT$(10, #1)
 PRINT #2, CopyString$