File I/O Model; Combining Modes & Having File Parameters Defined Outside of Function: Cause a Crash
up vote
0
down vote
favorite
local file_path = "Test.lua"
local file = io.open(file_path, "r")
local content = file:read("*a")
local file = io.open(file_path, "w")
function replace(string_id, value)
local string_id_search = tostring(string_id)
local string_id_reconfiguration = tostring(value)
local output = string.gsub(content, string_id_search, string_id_reconfiguration)
file:write(output) -- Says this is a closed file, why?
file:close()
end
This is currently my code . I have this repeated in a few other functions and I want to optimize it. So I want to first take out the first 3 local variables of the function and make the entire script have them as locals. Second, I want to combine the local file variable so it's just 1.
Problem 1) If I take the first 3 local variables out, it crashes saying the file:write(output) line is trying to write a closed file.
Problem 2) If I make the file mode "rw" or "wr" instead, it crashes as well. I tried all the other modes and got these results...
r does nothing (doesn't write the needed data, obviously)
w nukes all the data in the file (and writes nothing)
a does nothing
r+ does nothing
w+ nukes everything
a+ does nothing
Am I doing something wrong with the modes, or can they just not be combined?
file io lua
add a comment |
up vote
0
down vote
favorite
local file_path = "Test.lua"
local file = io.open(file_path, "r")
local content = file:read("*a")
local file = io.open(file_path, "w")
function replace(string_id, value)
local string_id_search = tostring(string_id)
local string_id_reconfiguration = tostring(value)
local output = string.gsub(content, string_id_search, string_id_reconfiguration)
file:write(output) -- Says this is a closed file, why?
file:close()
end
This is currently my code . I have this repeated in a few other functions and I want to optimize it. So I want to first take out the first 3 local variables of the function and make the entire script have them as locals. Second, I want to combine the local file variable so it's just 1.
Problem 1) If I take the first 3 local variables out, it crashes saying the file:write(output) line is trying to write a closed file.
Problem 2) If I make the file mode "rw" or "wr" instead, it crashes as well. I tried all the other modes and got these results...
r does nothing (doesn't write the needed data, obviously)
w nukes all the data in the file (and writes nothing)
a does nothing
r+ does nothing
w+ nukes everything
a+ does nothing
Am I doing something wrong with the modes, or can they just not be combined?
file io lua
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
local file_path = "Test.lua"
local file = io.open(file_path, "r")
local content = file:read("*a")
local file = io.open(file_path, "w")
function replace(string_id, value)
local string_id_search = tostring(string_id)
local string_id_reconfiguration = tostring(value)
local output = string.gsub(content, string_id_search, string_id_reconfiguration)
file:write(output) -- Says this is a closed file, why?
file:close()
end
This is currently my code . I have this repeated in a few other functions and I want to optimize it. So I want to first take out the first 3 local variables of the function and make the entire script have them as locals. Second, I want to combine the local file variable so it's just 1.
Problem 1) If I take the first 3 local variables out, it crashes saying the file:write(output) line is trying to write a closed file.
Problem 2) If I make the file mode "rw" or "wr" instead, it crashes as well. I tried all the other modes and got these results...
r does nothing (doesn't write the needed data, obviously)
w nukes all the data in the file (and writes nothing)
a does nothing
r+ does nothing
w+ nukes everything
a+ does nothing
Am I doing something wrong with the modes, or can they just not be combined?
file io lua
local file_path = "Test.lua"
local file = io.open(file_path, "r")
local content = file:read("*a")
local file = io.open(file_path, "w")
function replace(string_id, value)
local string_id_search = tostring(string_id)
local string_id_reconfiguration = tostring(value)
local output = string.gsub(content, string_id_search, string_id_reconfiguration)
file:write(output) -- Says this is a closed file, why?
file:close()
end
This is currently my code . I have this repeated in a few other functions and I want to optimize it. So I want to first take out the first 3 local variables of the function and make the entire script have them as locals. Second, I want to combine the local file variable so it's just 1.
Problem 1) If I take the first 3 local variables out, it crashes saying the file:write(output) line is trying to write a closed file.
Problem 2) If I make the file mode "rw" or "wr" instead, it crashes as well. I tried all the other modes and got these results...
r does nothing (doesn't write the needed data, obviously)
w nukes all the data in the file (and writes nothing)
a does nothing
r+ does nothing
w+ nukes everything
a+ does nothing
Am I doing something wrong with the modes, or can they just not be combined?
file io lua
file io lua
edited yesterday
Cœur
16.8k9101139
16.8k9101139
asked Sep 21 '14 at 4:19
B1313
211210
211210
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59
add a comment |
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25955739%2ffile-i-o-model-combining-modes-having-file-parameters-defined-outside-of-func%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It is that version. I don't understand what you mean that version. If I pull out the first 3 and run the script, it crashes. I don't know what you want (I will edit the OP to show what I mean). BTW did you delete the past comments?
– B1313
Sep 21 '14 at 6:07
You're describing a problem occurring inside the function, but this is impossible because the function is never called. In other words, you didn't provide all the relevant code. The problem may be in the code you didn't provided.
– Niccolo M.
Sep 21 '14 at 14:22
Another thing: a function should preferably do one thing only, and should be "self contained" if possible. Your function pokes at variables outside of it and it'd be hard for the programmer to ensure the function is called only when these outside variables are in the proper state.
– Niccolo M.
Sep 21 '14 at 14:26
The file is automatically loaded up upon the interpreter being opened (the interpreter is a game's engine). So it's running the script initially or initializing it (I would think). I want to use the variables outside the function because other functions use those same variables and rather then repeat them...I figured ok...let's make them local variables that can be used in the entire script. Unless I am doing it wrong.
– B1313
Sep 21 '14 at 18:59