Highlighted
Absent Member.
Absent Member.
1570 views

Reading Text File using .NET

Jump to solution

[Migrated content. Thread originally posted on 16 January 2012]

Hi,

Is there "a clever" .NET way to read a Windows .TXT file thats been created using say "NOTEPAD.EXE"

So my TXT file would contain 30 lines of varying bits of text?

Thanks in advance for any help.

Kind regards

Neil Willby.
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Reading Text File using .NET

Jump to solution
Hi Neil,

If what you mean by "clever" is using the .NET framework then you should look at the System.IO namespace and classes like StreamReader and File.

Example:

      $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.

          try
            *> 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
                else
                   display wsLine
                end-if
             end-perform   
           catch wsException
            *> Let the user know what went wrong.
             display "The file could not be read:"
             display wsException::Message
           end-try
           invoke sr::Close
           goback.


I personally don't find that to be any more clever or easier than using COBOL I-O statements like:


open input textfile

read textfile
   at end
      display "done"
end-read


but that's just me 🙂

View solution in original post

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Reading Text File using .NET

Jump to solution
Hi Neil,

If what you mean by "clever" is using the .NET framework then you should look at the System.IO namespace and classes like StreamReader and File.

Example:

      $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.

          try
            *> 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
                else
                   display wsLine
                end-if
             end-perform   
           catch wsException
            *> Let the user know what went wrong.
             display "The file could not be read:"
             display wsException::Message
           end-try
           invoke sr::Close
           goback.


I personally don't find that to be any more clever or easier than using COBOL I-O statements like:


open input textfile

read textfile
   at end
      display "done"
end-read


but that's just me 🙂

View solution in original post

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Reading Text File using .NET

Jump to solution
I prefer...


       01 #lines string occurs any.

       set #lines to type System.IO.File::ReadAllLines("debug.txt")
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.