|
Boost Users : |
From: Tim Boura (Tim.Boura_at_[hidden])
Date: 2006-05-22 19:43:08
Hi all.
I am trying to use the boost libraries for the first time and am running into some odd problems. This is not being helped by the fact that I am rather rusty in C++!
Quite simply I am writing out data to a file, which I need to have a header and footer on and then content in the center. When more data is appended to the file then it needs to be added in between the header and footer. In order to do this I check if the file exists before starting to write and only write the header in in that case - which works fine.
However if the file already exists then I need to open it for writing, and then rather than appending start from one line/10 characters before the end of the file and append. However if I open the file with any option other than 'out | app' then the file gets wiped, I have tried every combination of flags I can think of - even 'out & ~trunc' and nothing makes any difference.
From what I can see if I open it in 'out | app' then I can add things to the end of the file, but seek commands are ignored. If I open it in any mode other than app then the file gets wiped!
I've stripped it down a bit for simplicity but this is the code:
// Check whether the file already exists before creating it
old = boost::filesystem::exists(filename);
// Open file
boost::filesystem::wofstream file;
file.open( filename, std::ios_base::out /*| std::ios_base::app*/);
// Is this an existing file?
if (old)
{
// Move the write pointer to before the previous end tags
file.seekp(-10, std::ios_base::end);
}
else
{
// If this is a new file write the starting tags
file << L"HEADER" << std::endl << std::endl;
}
// Append the data
file << data << std::endl;
// Write the footer
file << L"FOOTER..." << std::endl;
// Close file
file.close();
Any suggestions greatfully appreciated, I know this is pretty basic stuff but I can't find any useful help files covering this as the ones I have say that what I am doing should work! Additionally if there is a better way to do this then that would be nice to know too :)
Thanks,
Tim
This email and any attachments is confidential, may be legally privileged and is intended for the use of the addressee only. If you are not the intended recipient, please note that any use, disclosure, printing or copying of this email is strictly prohibited and may be unlawful. If received in error, please delete this email and any attachments and confirm this to the sender.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net