Highlighted
Absent Member.
Absent Member.
2716 views

System.IO.FileStream

Jump to solution

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.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: System.IO.FileStream

Jump to solution

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)

View solution in original post

0 Likes
5 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: System.IO.FileStream

Jump to solution

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.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: System.IO.FileStream

Jump to solution

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?

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: System.IO.FileStream

Jump to solution

      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.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: System.IO.FileStream

Jump to solution

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

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: System.IO.FileStream

Jump to solution

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)

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.