i have small app @ begin read text file (with serialized objects) in im storing obejcts (im doing overloading << , >> operators). text file has updated each time new object created:
fstream m_haystackmapfile; m_haystackmapfile.open(haystackmapfile, std::ios::binary | std::ios::in | std::ios::out);
first read:
whaystackfile f; m_haystackmapfile.seekg(0, std::ios::beg); std::copy(std::istream_iterator<whaystackfile>(m_haystackmapfile), std::istream_iterator<whaystackfile>(), std::back_inserter(m_haystackfiles)); std::cout << "reading input file, no of files: " << m_haystackfiles.size() << std::endl; for(std::vector<whaystackfile>::iterator = m_haystackfiles.begin(); != m_haystackfiles.end(); ++it){ f = *it; std::cout << "key: " << f.getkey() << " cookie: " << f.getcookie() << " path: " << f.getpath() << " size: " << f.getsize()<< std::endl; }
then after creating new object write:
void whaystackmap::addentry(whaystackfile &f){ std::cout << "adding entry index file" << std::endl; m_haystackmapfile.seekp(std::ios::end); m_haystackmapfile << f; std::cout << f; }
unfortunatelly file in want write never updated , has size 0. maybe im messing something, after googling can't find answer how using fstream can read , write same file...
any welcomed :)
regards j.
the problem wrong usage of seekp()
:
- you use 1 single parameter
ios::end
inm_haystackmapfile.seekp(std::ios::end)
- but single parameter works absolute positionning.
ios::end
converted integer , locate @ unexpected place (on implementation it's 2). - you have use
m_haystackmapfile.seekp(0, std::ios::end)
instead
there problem: istream_iterator<>()
use in std::copy()
read stream until reaches end. failbit , eofbit set.
connsequently, no stream operation succed until clear flags: m_haystackmapfile.clear();