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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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