Ofstream problems

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 


Thank you in advance :)
Last edited on
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.
- Bjarne Stroustrup
Last edited on
write.close() so the file stops being in use.
however you have start Schedule.TXT, ¿is that file a batch script?
@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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>

std::ofstream write;//with this, it crashes program

int writehello ()
{
	write<<"hello\n";//write left undeclared without declaration above
}

int printschedule ()
{
	std::ofstream write ("Hello.TXT");
	write<<"Hi\n";
	writehello();
	
	write.close();
	system("start Hello.TXT");
	
	return 0;
}

int main()
{
	printschedule ();
	return 0;
}
Last edited on
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
Sorry for asking another question, is it more recommended to use a scope or use close function to stop it from writing?
Hello Happilicious,

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. :)
Last edited on
Hello Misenna,

I think i didn't explain my question clearly, sorry.
My question is as follows, both sample code are not defined globally:

1
2
3
4
5
//sample code 1
{
    std::ofstream write ("schedule.TXT");
    write<<"Hello";
}//scoping to stop writing 

or
1
2
3
4
//sample code 2
std::ofstream write ("schedule.TXT");
write<<"Hello";
write.close();//to stop writing 


Which attempt is better to stop writing? And why?

Topic archived. No new replies allowed.