Problems outputting to .txt

May 30, 2011 at 4:04pm
1
2
3
4
5
6
7
8
9
10
11
int writeFile (int prime){
	ofstream outputFile ("primary.txt");
	outputFile.open ("primary.txt", ios::out | ios::app);
	outputFile << prime << "\n";

	outputFile.close();
	cout << "Done!\n";

		cin.get();
return 0;
} 


After I run the function nothing has changed in the .txt. I really don't know what the problem is, which probably is staring at me laughing.

I want to add numbers to the text, so to make a list.
Any ideas anyone?
May 30, 2011 at 6:03pm
Why does it return an int? There seems to be no reason for this function to not have a void return type. Also, you're opening the file twice, once in line 2 and then again in line 3. I removed the ("primary.txt") from line 2 and it works fine now. I can't exactly tell you why, but I guess opening the file twice like that does something that makes the program not write to it properly.
May 31, 2011 at 8:48am
yeah, the int-return 0; is a bad habit of mine, I'm sorry to say.

Thanks for the help, removing the () and contents worked. Would love to know why, but right now I'm glad you caught the miss.

Thanks again.
Topic archived. No new replies allowed.