Reputation: 943
I currently do this to write the contents of an ostringstream
to a file:
... // -- Loop 1 Start
std::ostringstream osStr;
... // -- Loop 2 Start
cv::string s(osStr.str());
std::istringstream iss(s);
cv::vector<cv::string> subVec;
do{
cv::string sub;
iss >> sub;
subVec.push_back(sub);
} while (iss);
cv::FileStorage fs("subVec.txt", cv::FileStorage::WRITE);
fs << "subVec" << subVec;
fs.release();
... // -- Loop 2 End
... // -- Loop 1 End
but this overwrites what was previously in the file.
So I moved the cv::FileStorage fs("subVec.txt", cv::FileStorage::WRITE);
to before the loop, and fs.release
to after the loop.
cv::FileStorage fs("subVec.txt", cv::FileStorage::WRITE);
... // -- Loop 1 Start
std::ostringstream osStr;
... // -- Loop 2 Start
cv::string s(osStr.str());
std::istringstream iss(s);
cv::vector<cv::string> subVec;
do{
cv::string sub;
iss >> sub;
subVec.push_back(sub);
} while (iss);
fs << "subVec" << subVec;
... // -- Loop 2 End
... // -- Loop 1 End
fs.release();
This worked in a way as it did continuously write to the .txt
, but was unusable due to Duplicated Keys:
%YAML:1.0
subVec:
- "1"
- "2"
...
- "33"
- "34"
subVec:
- "1"
- "3"
...
- "30"
- "31"
See how subVec is repeated
I'm looking for a way to save the contents of the ostringstream
to a file where it doesn't overwrite the contents each time, but is actually readable.
OpenCV Error: Parsing error (subVec.txt(27): Duplicated key) in cvGetFileNode
Upvotes: 1
Views: 6010
Reputation: 943
As there is little response, I'll have to settle with this fix:
cv::vector<cv::string> subVec; // -- MOVED
cv::FileStorage fs("subVec.txt", cv::FileStorage::WRITE);
... // -- Loop 1 Start
std::ostringstream osStr;
... // -- Loop 2 Start
cv::string s(osStr.str());
std::istringstream iss(s);
do{
cv::string sub;
iss >> sub;
subVec.push_back(sub);
} while (iss);
... // -- Loop 2 End
... // -- Loop 1 End
fs << "subVec" << subVec; // -- MOVED
fs.release();
Although it does leave me with the issue of cv::vector<cv::string>
being able to only hold so much.
A fix for this would be good!
Upvotes: 1
Reputation: 454
If it helps, I open a std::ofstream m_OutFile
member at class level, open it with m_OutFile.open(file.c_str(), std::ios::out);
in a constructor call. I also give it a buffer to save hard drive access:
m_OutFile.rdbuf()->pubsetbuf(m_CharBuffer,BUFFER_SIZE);
and then whenever I want to write to it in a function, I use
std::ostringstream oStr;
oStr << "some text and things" << std::ends;
m_OutFile << oStr.str() << std::endl;
Works neat for me.
Upvotes: 2