Reputation: 3832
struct GPattern() {
int gid;
....
}
class Example() {
public:
void run(string _filename, unsigned int _minsup);
void PrintGPattern(GPattern&, unsigned int sup);
....
};
Eample::run(string filename, unsigned int minsup) {
for(...) { // some condition
// generate one GPattern, and i want to ouput it
PrintGPattern(gp, sup);
}
}
Example::PrintGPattern(GPattern& gp, unsigned int sup) {
// I want to ouput each GPattern to a .txt file
}
run
is used to generate a GPattern
accordingly.
What I want to output to a file is some texts that reconstruct the original GPattern
.
I can't store all GPattern
in advance and output all of them. I have to output one GPattern
to a file when I generate it, but I don't know how to implement it.
I have tried to declare ofstream outGPatter("pattern.txt")
in class Example
, but it is of no use...
Upvotes: 0
Views: 116
Reputation: 23537
The way I see it, you want to append information of multiple GPattern
and you simply need to set the I/O mode to ios::app
in the constructor.
struct GPattern {
int gid;
friend ostream& operator <<(ostream& o, GPattern gp) {
return o << "(gid=" << gp.gid << ")";
}
...
}
Example::PrintGPattern(GPattern& gp, unsigned int sup) {
ofstream fout("pattern.txt", ios::app)
fout << gp << endl;
fout.close()
}
Upvotes: 1
Reputation: 176
I think you can use append mode, such as:
ofstream outGPattern;
outGPattern.open("GPattern.txt", ios::app);
Upvotes: 1
Reputation: 1452
Well, ofstream is the right way to go:
Example::PrintGPattern(GPattern& gp, unsigned int sup) {
ofstream outGPattern("pattern.txt")
outGPattern << gp.gid; << " " << gp.anotherGid << " " ....
outGPattern.close()
}
Have you looked at the correct place for the pattern.txt? It should either be in the folder where your .exe is, or in the folder where all your .h and .cpp files are (for VS at least).
If you want to write all patterns into the same file then you need to make sure you append (and not overwrite) the pattern.txt
ofstream outGPattern("pattern.txt",ios::app)
So you can first make an ofstream without ios::app (to clear the textfile) at the start of your program. Then you construct all other ofstreams with ios::app to append new text, instead of overwriting it.
Alternatively you can make the ofstream a member variable of Example. Then you construct it only once.
Upvotes: 1