• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
      • Std/lists
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
      • Std/osets
      • Std/io
        • Open-channel-lemmas
        • Std/io/read-char$
        • Std/io/read-object
        • Std/io/open-output-channel
        • Unsound-read
        • Read-string
        • Read-bytes$
        • File-measure
        • Read-bytes$-n
        • Std/io/read-byte$
        • Std/io/open-input-channel
        • Read-file-lines-no-newlines
        • Print-compressed
        • Nthcdr-bytes
        • Read-file-lines
          • Read-file-lines-aux
        • Std/io/close-output-channel
        • Read-file-characters
        • Read-file-bytes
        • Print-legibly
        • Std/io/close-input-channel
        • Read-file-objects
        • Logical-story-of-io
        • Take-bytes
        • Std/io/peek-char$
        • Read-file-characters-rev
        • Read-file-as-string
        • Std/io/write-byte$
        • Std/io/set-serialize-character
        • Std/io/print-object$
        • Std/io/princ$
        • Std/io/read-file-into-string
        • *file-types*
      • Std/basic
      • Std/system
      • Std/typed-lists
      • Std/bitsets
      • Std/testing
      • Std/typed-alists
      • Std/stobjs
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Math
    • Testing-utilities
  • Std/io

Read-file-lines

Read an entire file into a list of lines (strings).

Signature: (read-file-lines filename state) returns (mv contents state).

On success, contents is a string-listp that contains each line of the file. Each string except the last will end with a newline character, i.e., #\Newline. For a variant of this utility that omits these newlines, see read-file-lines-no-newlines.

On failure, e.g., perhaps filename does not exist, contents will be a stringp saying that we failed to open the file.

BOZO This currently just looks for individual newline characters, i.e., #\Newline, sometimes called \n. It might be desirable to change how it works to somehow support \r\n or whatever other carriage-return stuff people use on platforms like Windows.

Definitions and Theorems

Function: read-file-lines

(defun read-file-lines (filename state)
  "Returns (MV ERRMSG/LINES STATE)"
  (declare (xargs :guard (stringp filename)
                  :stobjs state))
  (b* ((filename (mbe :logic (if (stringp filename) filename "")
                      :exec filename))
       ((mv channel state)
        (open-input-channel filename
                            :byte state))
       ((unless channel)
        (mv (concatenate 'string
                         "Error opening file " filename)
            state))
       ((mv data state)
        (read-file-lines-aux nil nil channel state))
       (state (close-input-channel channel state)))
    (mv (reverse data) state)))

Theorem: state-p1-of-read-file-lines

(defthm state-p1-of-read-file-lines
  (implies (force (state-p1 state))
           (state-p1 (mv-nth 1 (read-file-lines filename state)))))

Theorem: string-listp-of-read-file-lines

(defthm string-listp-of-read-file-lines
  (equal
       (string-listp (mv-nth 0 (read-file-lines filename state)))
       (not (stringp (mv-nth 0 (read-file-lines filename state))))))

Subtopics

Read-file-lines-aux
Tail recursive implementation of read-file-lines.