sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Writing to specific lines in a File

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 2 Posters 97 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      Lets say I have a file with 100 lines. I would like to open that file, and insert a line before line X (lets say line 50). Is that possible without loading the whole file into an array with readlines?

      Chris

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Good question... never tried it.

        You can set the current line via lineno=(), but I suspect these IO file functions (which File inherits,) are sequential files, and using puts() after setting the line, is likely to overwrite that line.

        Perhaps you may wish to look in the standard Ruby Extended libs for a Random Access File extension ??

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          OH !! Chris ... didn't realize this.. there's a TempFile manager class in the standard Ruby library.

          Also there's the PStore class, which is like a file-based Hash.

          There's also a "massive" CSV class.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            Awesome Dan, thanks! I'll go look into those.

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement