Good day everyone,
I have a problem in my program, when i use ofstream called write, main.exe starts to not respond, and crashes. If i copy ofstream part to a new console app, it works. But i can't find where i did wrong. Solutions and code improvement suggestions are appreciated. Code link (edited): https://pastebin.com/uCnJ4wuX
2nd ofstream problem is i cant get system to open it because access denied. How am i able to stop ofstream write in order to open the text? How many ways to solve this problem?
1 2 3
std::ofstream write ("Schedule.TXT");
write<<"Thank you for using Fitness Optimizer NEUTRONIUM7000.\n";
system("start Schedule.TXT");//access denied
you have a problem with one specific part of your program, so instead of linking to c.500 lines of code it'd be easier all around to focus on that :
I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
@gunnerfunner, I'll try to debug myself first since I just had a rest, maybe my mind will work better this time around, thank you for the suggestion. If I really can't figure it out, I would really need help on debugging it. Can you share your experiences perhaps why sometimes ofstream causes main.exe not responding?
@ne555, it is not a batch script, it is a text file called Schedule. Is it wrong to use that method to open a text file? If it's a hint, it doesn't tick a bell for me which method I need to use, I'm sorry. After I added write.close() in it, i was able to open the text file, thank you, I'll research more on it :)
(edit)I'm done debugging :D
by doing ofstream declaration, it crashes the program. But i need it since i have function that left write declared. How should i solve this problem?
you have two different ofstream objects, they are both named `write' but one is global an the other is local to printschedule().
you never open the global `write', and writehello() tries to put "hello\n" in it and so the crash.
I would suggest you to pass the ofstream as a parameter to the functions
1 2 3 4 5 6 7 8 9 10 11 12
void writehello(std::ofstream &write){ //note that the ofstream is passed by reference
write << "hello\n";
}
void printschedule(std::ofstream &write){
write << "Hi\n";
writehello(write);
//...
}
int main(){
std::ofstream output("Hello.txt");
printschedule(output);
}
I'm having a mind blown moment as I didn't know ofstream can be passed like so :o
It does make sense since i also pass string around, just not with ofstream before.
Thank you very much @ne555 :D
Anything you define globally is available globally. There are situations where it seems to be inevitable to use something in global space. It should be avoided whenever possible. This is something that every good programming book, and/or teacher for that matter, would tell you. Define whatever your program needs in scope, local to functions, close to loops even, helps avoid trouble in the long run. Trouble, for instance, when several different functions read/write to or from a global variable. And I guess more experienced programmers can give you several more reasons why the use of global functions is best to be avoided. And several good ones at that. So, pay attention, use them cautiously, and if you have to, use the const qualifier for variables and what have you, so that they be at least read only. :)