Created On:  16 January 2012


How does one read a text file using the .NET Framework classes in Visual COBOL?


You should look at the System.IO namespace and classes like StreamReader and File in order to accomplish the task of reading a text file in a variety of different ways.

Example #1:

      $set ilusing"System.IO"
       program-id. Program1 as "testreadtext.Program1".
       data division.
       working-storage section.
       01 sr           type StreamReader.
       01 wsLine       string.
       01 wsException  type Exception.
       procedure division.

            *> Create an instance of StreamReader to read from a file.
            set sr to new type StreamReader("testfile.txt")
            *> Read and display lines from the file until the end of
            *> the file is reached.
             perform until exit
                set wsLine to sr::ReadLine
                if wsLine = null
                   exit perform   *>EOF reached
                   display wsLine
           catch wsException
            *> Let the user know what went wrong.
             display "The file could not be read:"
             display wsException::Message
           invoke sr::Close

Example #2:

01 #lines string occurs any.

    set #lines to type System.IO.File::ReadAllLines("debug.txt")

Example #3:

Standard COBOL I-O still works fine.

open input textfile

read textfile
   at end
      display "done"