ofstream troubleshooting

I'm having trouble with my code outputting to a file. It was doing fine before, then it recently just quit outputting whenever I run the program. Am I missing something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
outfile.open("C:\\Temp\\outset.dat", ios::app);
	outfile << "{}";
	cout << numberOfInputs << endl;


	for (int i = 0; i < numberOfInputs; i++)
	{
		cout << "OK" << endl;
		outfile << "{";
		for (int c = 0; c < 1; c++)
		{
			outfile << fileInput[i];
		}
		outfile << "}";
	}


I have ofstream declared as "outfile" in the class.
It's passing into the loop, I can see it through the console output. However, when I open outset.dat, its empty.

Thanks
std::ofstream, if I recall, maintains a buffer internally. Do you ever flush it, like by calling outfile.close()? Just a shot in the dark.

-Albatross
When I was testing it, I never flushed it. However, when it wasn't writing to the file, I tried that and the results stayed the same. I tried moving "ofstream outfile" inside the function and it seemed to work again.
I just don't understand why it wouldn't work when I declared it in the class (especially the randomly quit working part).

Thanks
I honestly am not sure what you're talking about because you're mentioning significant pieces of your code that you haven't posted and that I therefore cannot see. If you could post all the relevant pieces of code, that would help us help you greatly.

-Albatross
Calculation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

using namespace std;

#pragma once
class Calculation
{
public:
	Calculation();
	~Calculation();
	void LoadFile();
	// Compute and Output Power Set
	int PowerSet();
	// Computer Set Cardinality
	int SetCardinality();
	// Computer Power Set Cardinality
	int PowerSetCardinality();

	ofstream outfile;    //<-----Wasn't working when declared here
	ifstream infile;
	int fileInput[21];
	int numberOfInputs=0;
};


Snippet of Calculation.cpp
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
28
int Calculation::PowerSet()
{
	ofstream outfile;  //<----Had to add this here to make it work.
	outfile.open("C:\\Temp\\outset.dat", ios::app);
	outfile << "{}";
	if (numberOfInputs > 1)
		outfile << ", ";
	cout << numberOfInputs << endl;

	int counter = 0;
	int comma = 0;
	int outControl = 0;
////////////////////////////////////////////////////////

	for (int i = 0; i < numberOfInputs; i++)
	{
		outfile << "{";
		for (int c = 0; c < 1; c++)
		{
			outfile << fileInput[i];
			outControl = outControl + i + c;
		}
		outfile << "}, ";
		if (outControl == 3)
		{
			outfile << endl;
		}
	}


Sorry for the lack of information.

outfile.close() is now included later in the program.

Is it possible I just messed the buffer up by running it so many times before without outfile.close()?
Topic archived. No new replies allowed.