Skip to main content

Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

Here is an example that creates a new file one byte at a time using FileStream and then reads it back one byte at a time using FileStream::ReadByte to verify what was written.

      $set ilusing"System.IO" 
       program-id. Program1 as "streambyteread.Program1".

       data division.
       working-storage section.
       78 filename    value "C:\\temp\\newfile.txt".
       procedure division.

           *> Create random data to write to the file. 
           declare dataArray as type Byte occurs 1000
           invoke new Random::NextBytes(dataArray)

           perform using fileStream as type FileStream = new FileStream(fileName, type FileMode::Create)
              *> Write the data to the file, byte by byte. 
              perform varying i as binary-long from 0 by 1 until i = dataArray::Length
                 invoke fileStream::WriteByte(dataArray)
              end-perform
              *> Set the stream position to the beginning of the file.
              invoke fileStream::Seek(0, type SeekOrigin::Begin)
              *> Read and verify the data. 
              perform varying i as binary-long from 0 by 1 until i = fileStream::Length
                 if dataArray not = fileStream::ReadByte()
                    display "Error writing data."
                    goback
                 end-if
              end-perform
              display "The data was written to " & fileStream::Name & " and verified."
           end-perform
           goback.
           
       end program Program1.

Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

Hi Chris,

Thanks for that.

Between what you gave me above and what I found on the internet I came up with the following code. Do you think this is the most efficient way of reading a flat file using FileStream?


Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

      method-id btnGo_Click final private.

      01  ls-CharsRead pic 9(09).

      01  ls-CharsReadx redefines ls-CharsRead.

          03  filler   pic 9(06).

          03  ls-Chars1000 pic 9(03).

      procedure division using by value sender as object e as type System.EventArgs.

          set btnGo::Enabled to false

          set ws-cFileStream to type System.IO.FileStream::New(txtFileName::Text::Trim(" "), type System.IO.FileMode::Open, type System.IO.FileAccess::Read)

          invoke ws-cFileStream::Seek(0, type System.IO.SeekOrigin::Begin)

          declare numBytesToRead as type System.Int32

          set numBytesToRead to ws-cFileStream::Length

          set txtTotalChars::Text to numBytesToRead

          declare numBytesRead as type System.Int32

          set numBytesRead to zero

          declare n as type System.Int32

          declare b as type Byte

          perform until numBytesToRead = 0

              set b to ws-cFileStream::ReadByte()

     *        set txtImport::Text to string::Concat(txtImport::Text::Trim(" "), " ", b)

              compute numBytesRead = (numBytesRead 1) end-compute

              compute numBytesToRead = (numBytesToRead - 1) end-compute

              set ls-CharsRead to numBytesRead

     *        if ls-Chars1000 = zeros

     *            set txtCharsRead::Text to numBytesRead

     *            invoke type System.Windows.Forms.Application::DoEvents()

     *        end-if

          end-perform

          set txtCharsRead::Text to numBytesRead

          set btnGo::Enabled to true

      end method.


Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

Above works on a Button Click called "BthGo" then reads the import file which is over 1MB in size one byte at a time.


Hi,

Does any one have a Visual Cobol example of how to read a text file one character at a time using the System.IO.FileStream method so I can display the data in a TextBox ?

Thanks in advance for any assistance.

Kind regards

Neil.

There are many ways to do this but a simple way would be to use the Read method to handle the entire file in a single operation instead of a byte at a time.

Example:

          declare ws-cFileStream as type FileStream = new FileStream("mydata.txt", type FileMode::Open, type FileAccess::Read)
          invoke ws-cFileStream::Seek(0, type SeekOrigin::Begin)
          declare bytes as binary-char unsigned occurs any
          set size of bytes to ws-cFileStream::Length
          invoke ws-cFileStream::Read(bytes, 0, ws-cFileStream::Length)
          declare textfield as string = type System.Text.Encoding::Default::GetString(bytes)