Jump to content

_FileWriteToLine Pitfall


Funtime60
 Share

Recommended Posts

I'm pretty sure this isn't the first time I've ran into this issue with _FileWriteToLine. I was trying to edit a large file in place without creating a temporary file. I tried to use _FileWriteToLine as I needed to replace a specific lines with a line of equal length. The issue I ran into is that _FileWriteToLine both reopens the file, then reads it entirely into an array, thus defeating the purpose I was using it for. (Which IIRC isn't mentioned in the help file). In fact I can think of few cases where it isn't just better to do what _FileWriteToLine is doing manually.

TL:DR if you want to REPLACE characters in a file without loading the entire file into memory, you'll need to use FileSetPos and FileWriteLine.

In case it wasn't clear, I'm writing this so that others and future me don't make the same mistake.

Link to comment
Share on other sites

  • Moderators

Funtime60,

Quote

_FileWriteToLine both reopens the file, <...>. (Which IIRC isn't mentioned in the help file)

The Help file does mention that "A file may fail to open due to access rights or attributes"- which to me implies quite clearly that the function does open files when used.

And just how do you intend to modify a file without reading it into memory? Rewrite the values stored on the disk directly? Sounds like a quick route to disaster to me!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

3 hours ago, Funtime60 said:

TL:DR if you want to REPLACE characters in a file without loading the entire file into memory, you'll need to use FileSetPos and FileWriteLine.

Thank you for the tip, but I think overwriting would be the better term. Also you need to know the position for FileSetPos beforehand, as FileSetPos is reffering to the actual position in a file and not a line.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...