[Closed] How to delete a line from an external file?
must not be closing the file handle entirely somewhere, then – are you opening it twice, perhaps? opening it in a loop? etc.
-- create a temp file, stick 3 lines in it, close the file
f = createFile "$temp\ emp.txt"
format "First line
Second line
Third line" to:f
close f
-- open the file, read all lines from it and print them, close the file
f = openFile "$temp\ emp.txt"
while (not eof f) do ( print (readline f); OK )
close f
-- "First line"
-- "Second line"
-- "Third line"
-- delete the 2nd line
deleteLine (getDir #temp + "\ emp.txt") 2
-- do the read+print thing again
f = openFile "$temp\ emp.txt"
while (not eof f) do ( print (readline f); OK )
close f
-- "First line"
-- "Third line"
I looked at it again and I figured out that I was opening it twice but I thought that closing it once would do the trick regardless of how many times it was opened prior. But now that I think about it – it’s a bit sloppy to open the same file twice.
What I have is createFile function as well as openFile function for the newly created file. I don't really need the openFile after the create because apparently it is already open for reading/writing. :shrug: Learn something everyday.
I just want to know one last thing though:
g = maxfilepath + "text.txt"
"C: ext.txt"
(dotNetClass "System.IO.File").Create g
dotNetObject:System.IO.FileStream
(dotNetClass "System.IO.File").ReadAllLines g
-- Runtime error: dotNet runtime exception: The process cannot access the file 'C: ext.txt' because it is being used by another process.
In this case do I need to close the file after creating it to read it? How would I do it?
In “msdn.microsoft.com” it says that
The [FileStream]( http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx) object created by this method has a default [FileShare]( http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx) value of [None]( http://msdn.microsoft.com/en-us/library/system.io.fileshare.none.aspx); no other process or code can access the created file until the original file handle is closed.
While down the line it also says:
By default, full read/write access to new files is granted to all users. The file is opened with read/write access and must be closed before it can be opened by another application.
BTW you can’t use “lines” as a variable – it turns out to be a reserved word for maxscript.
Use something like…
local myFile = (dotNetClass "System.IO.File").Create g
myFile.close()
(dotNetClass "System.IO.File").ReadAllLines g
Sure I can! I can do Anything! sets ‘local gravity = 0.0’, jumps off cliff and drifts away
But you’re right, you should typically avoid it, even though overriding it with a local is usually just fine – except that you won’t be able to access the reserved version within that scope anymore. In this case, the file thing probably doesn’t much care about the objectgroup that is ‘lines’. Usually I do change the variables from ‘thing’ to ‘myThing’ or ‘_thing’, whichever is more appropriate.
Thanks a lot for your help.
Better keep that gravity var a local – I just got used to the 9.8