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.
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).
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.
#include <iostream>
#include <fstream>
usingnamespace 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;
};
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()?