Writing to specific lines in a File
-
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
-
Good question... never tried it.
You can set the current line via
lineno=(), but I suspect theseIOfile functions (whichFileinherits,) are sequential files, and usingputs()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 ??
-
OH !! Chris ... didn't realize this.. there's a
TempFilemanager class in the standard Ruby library.Also there's the
PStoreclass, which is like a file-basedHash.There's also a "massive"
CSVclass. -
Awesome Dan, thanks! I'll go look into those.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better đź’—
Register LoginAdvertisement