using << operator
I have a pointer to an ofstream object, and I am trying to use it to write to a file using the << operator, but it wont work. Here is the code I am using:
But it doesnt work. The file has been created but it wont write anything to it. Is there sonething obvious I am doing wrong??
// in header file
// in cpp file
logfile = new ofstream("logfile.txt")
*logfile << "logging enabled\n";
I'm really bad with pointers, but...
So you're writing to the adress of the pointer rather then the pointer object?
Or try simply,
Once again, I'm no good shot at pointers, so I may be totaly off-track.
logfile << ofstream
I would think that the file is not closed properly. Did you delete logfile ?
The problem was that the file was not being closed in the program.
Which is why you should allocate such things as files either on the stack, or use a smart pointer for them. (Actually, you should use smart pointers for nearly every dynamic allocation.)
Do you really need a pointer to an ofstream object? You could create an ofstream object, and then use its open() member function at some later point, after all.