本文整理汇总了C++中std::fstream::seekp方法的典型用法代码示例。如果您正苦于以下问题:C++ fstream::seekp方法的具体用法?C++ fstream::seekp怎么用?C++ fstream::seekp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::fstream
的用法示例。
在下文中一共展示了fstream::seekp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _WriteBlock
void Database::_WriteBlock(Item towrite, uint64_t blockpos) {
backing.seekg(blockpos);
backing.seekp(blockpos);
uint64_t blkhdrs[4];
if (backing.eof()) {
blkhdrs[0] = 0;
blkhdrs[1] = towrite.itemsize;
blkhdrs[2] = towrite.itemsize;
blkhdrs[3] = towrite.itemsize + (sizeof(uint64_t)*4) + 1;
backing.write(0, 1);
backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
backing.write(towrite.item, towrite.itemsize);
backing.flush();
} else {
_GetLock(blockpos, true);
backing.read((char*)blkhdrs, sizeof(uint64_t)*4);
if (towrite.itemsize + 1 + (sizeof(uint64_t)*4) <= blkhdrs[3]) {
//Block large enough or non-existent block
backing.write(0, 1);
blkhdrs[0] = 0;
blkhdrs[1] = towrite.itemsize;
blkhdrs[2] = towrite.itemsize;
//Keep blkhdrs[3]
backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
backing.write(towrite.item, towrite.itemsize);
backing.flush();
_ReleaseLock(blockpos, true);
} else {
//Have to continue to a new block, allocate or reuse
std::streampos currentpos = backing.tellp();
backing.seekp(0, std::ios::end);
std::streampos endpos = backing.tellp();
backing.seekp(currentpos, std::ios::beg);
blkhdrs[0] = (uint64_t) endpos;
//Keep blkhdrs[1]
blkhdrs[2] = towrite.itemsize;
//Keep blkhdrs[3]
uint64_t blockcancontain = blkhdrs[3] - ((sizeof(uint64_t)*4) + 1);
backing.write(0, 1);
backing.write((char*)&blkhdrs, sizeof(uint64_t)*4);
backing.write(towrite.item, blockcancontain);
backing.flush();
_ReleaseLock(blockpos, true);
//Continue to next block!
Item process;
process.itemsize = towrite.itemsize-blockcancontain;
process.item = towrite.item+blockcancontain;
_WriteBlock(process, blockpos);
}
}
}
示例2: writeAt
void IndexDataStructure::writeAt(std::fstream & _output_file, std::streampos _position)
{
// remember the write file position
std::streampos w_pos = _output_file.tellp();
// go to asked write position
_output_file.seekp(_position);
writeToNext(_output_file);
// restore original write position
_output_file.seekp(w_pos);
}
示例3: writeUNPW
int User::writeUNPW(std::fstream &fout){
if (!fout.is_open()){
fout.open("UNPW.bin", std::ios::out, std::ios::binary);
}
auto fpos = fout.cur;
fout.seekp(0, fout.beg);
encode(username);
encode(password);
fout.write((char*)&username, sizeof(str));
fout.write((char*)&password, sizeof(str));
decode(username);
decode(password);
fout.seekp(0, fpos);
return 0;
}
示例4: erase
uint64_t erase (const size_t offset, std::fstream &lib)
{
// ar header format:
// offset, length, description
// 0 16 File name, ASCII
// 16 12 File modification timestamp, Decimal
// 28 6 Owner ID, Decimal
// 34 6 Group ID, Decimal
// 40 8 File mode, Octal
// 48 10 File size in bytes, Decimal
// 58 2 File magic, 0x60 0x0A
// we will use this a lot. this are right-padded.
const char zero = 0x30;
const char pad = 0x20;
// first of all, archive creation timestamp goes to zero
lib.seekp (static_cast<int64_t> (offset + 16));
lib.put (zero);
for (size_t i = 0; i < 11; ++i)
lib.put (pad);
// now the uid:
lib.seekp (static_cast<int64_t> (offset + 28));
lib.put (zero);
for (size_t i = 0; i < 5; ++i)
lib.put (pad);
// and gid
lib.seekp (static_cast<int64_t> (offset + 34));
lib.put (zero);
for (size_t i = 0; i < 5; ++i)
lib.put (pad);
// return the size:
lib.seekg (static_cast<int64_t> (offset + 48));
char size[10];
lib.read (size, 10);
for (ssize_t i = 9; i >= 0; --i) {
if (size[i] == pad)
size[i] = '\0';
}
uint64_t ret = static_cast<uint64_t> (std::atol (size));
if (ret % 2 != 0)
++ret; // everything is aligned to uint16_t, but file size is the
// actual file size.
ret += 60; // add header size
return ret;
}
示例5: _ReleaseLock
void Database::_ReleaseLock(uint64_t blockpos, bool readonly) {
if (readonly) {
char blocklock = 0;
backing.seekp(blockpos);
backing.write(&blocklock, 1);
}
}
示例6: recvfile
long long TCPTransport::recvfile(std::fstream& ofs, long long offset, long long size)
{
if (!m_bConnected)
return -1;
if (ofs.bad() || ofs.fail())
return -1;
ofs.seekp(offset);
int block = 1000000;
char* buf = new char[block];
long long recd = 0;
while (recd < size)
{
int unit = int((size - recd) > block ? block : size - recd);
recv(buf, unit);
ofs.write(buf, unit);
recd += unit;
}
delete [] buf;
return recd;
}
示例7: encryptData
bool XorEncryptor::encryptData(std::fstream &original, std::fstream &result)
{
if (!original.is_open() || !result.is_open())
{
return false;
}
original.seekg(0, std::ios::beg);
result.seekp(0, std::ios::beg);
char c = 0;
unsigned i = 0;
while (original.good())
{
original.read(&c, 1);
c ^= password[i];
if(original.gcount() > 0)
{
result.write(&c, 1);
}
if (++i == passSize)
{
i = 0;
}
}
original.seekg(0, std::ios::beg);
result.seekg(0, std::ios::beg);
result.flush();
return true;
}
示例8:
bool
StandardFileProvider::seek( Size pos )
{
if( _seekg )
_fstream.seekg( pos, ios::beg );
if( _seekp )
_fstream.seekp( pos, ios::beg );
return _fstream.fail();
}
示例9: writeBlockToFile
BlockOffset writeBlockToFile(Block b, std::fstream& f) {
BlockOffset off = f.tellp();
f.write(reinterpret_cast<char *>(&b.data), sizeof(b.data));
for(auto& o : b.offsets) {
f.write(reinterpret_cast<char *>(&o), sizeof(o));
}
f.seekp(off, f.beg);
return off;
}
示例10: checkformodifications
void resultsfile::checkformodifications(std::fstream& file)
{
assert(file.good());
trace << "DEBUG (resultsfile): checking file for modifications." << std::endl;
// check for user modifications
sha curdigest;
file.seekg(0);
curdigest.process(file);
// reset file
file.clear();
if (curdigest == filedigest)
file.seekp(fileptr);
else
{
cerr << "NOTICE: file modifications found - appending." << std::endl;
// set current write position to end-of-file
file.seekp(0, std::ios_base::end);
fileptr = file.tellp();
}
}
示例11: runtime_error
void filesystem::supercluster::save(std::fstream& s, uint32_t index)
{
uint64_t offset = SUPERCLUSTER_SIZE * index;
char buffer[CLUSTER_SIZE];
for(unsigned i = 0; i < CLUSTERS_PER_SUPER; i++)
serialization::u32b(buffer + 4 * i, clusters[i]);
s.clear();
s.seekp(offset, std::ios_base::beg);
s.write(buffer, CLUSTER_SIZE);
if(!s)
throw std::runtime_error("Can't write cluster table");
}
示例12: serialize
int serialize(linked_list<T>& list, std::fstream& out) {
if(!out.is_open())
throw serialize_exception();
serialize(LINKEDLIST_SERIAL_ID, out);
int byte_count_pos = out.tellp();
out.seekp(sizeof(int), std::ios::cur);
int byte_count = 0;
byte_count += serialize(list.list_size, out);
for(typename linked_list<T>::link* current = (list.head)->next; current != NULL; current = current->next)
byte_count += serialize(current->element, out);
int end_pos = out.tellp();
out.seekp(byte_count_pos);
serialize(byte_count, out);
out.seekp(end_pos);
return byte_count + sizeof(int) + sizeof(long); // content + header
}
示例13: WriteSector
void WriteSector(char* buffer, size_t sec) {
sec += partitionOffset;
if (sec > partitionLength)
return;
if ((sec * 512) > fsize) {
std::cout << "WOAH BRO THIS SECTOR IS WRONG: " << sec << std::endl;
return;
}
f.seekp(sec * 512, std::ios::beg);
f.write(buffer, 512);
}
示例14: writeToNext
void IndexDataStructure::writeToNext(std::fstream & _output_file)
{
m_is_indexed = true;
m_index_position = _output_file.tellp();
_output_file.write((char *)(&m_count), sizeof(m_count));
_output_file.write((char *)(m_keys), sizeof(m_keys[0]) * m_max_count);
_output_file.write((char *)(m_addresses), sizeof(m_addresses[0]) * m_max_count);
_output_file.write((char *)(&m_next_sibling_address), sizeof(m_next_sibling_address));
// skip free block space
_output_file.seekp(m_block_size - m_real_data_size, _output_file.cur);
}
示例15: _GetLock
void Database::_GetLock(uint64_t blockpos, bool readonly) {
char blocklock = 1;
backing.seekg(blockpos);
backing.read(&blocklock, 1);
while (blocklock == 1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(50)); //Thanks StackOverflow!
backing.seekg(blockpos);
backing.read(&blocklock, 1);
}
if (readonly) {
blocklock = 1;
backing.seekp(blockpos);
backing.write(&blocklock, 1);
}
}