简体   繁体   English

C ++ std :: ofstream - 移动put指针

[英]C++ std::ofstream - Move the put pointer

I am writing some data to a file. 我正在写一些数据到文件。 Occasionally, I want to write a block of data from memory, and then move the put pointer along either 1, 2 or 3 bytes to maintain a 4 byte data boundary format. 有时,我想从内存中写入一个数据块,然后沿着1,2或3个字节移动put指针以保持4字节数据边界格式。

I could make a new block of data containing zeros and write this, but this seems unnecessary and clumsy. 我可以创建一个包含零的新数据块并写出来,但这似乎是不必要和笨拙的。 How can I move the put pointer along 1, 2 or 3 bytes? 如何将put指针沿1,2或3个字节移动?

I am not sure how to do this, because if I call seekp() surely I will move the pointer outside of the current file size? 我不知道该怎么做,因为如果我调用seekp()肯定会将指针移到当前文件大小之外? Whereas I assume ofstream.write() deals with this correctly? 虽然我假设ofstream.write()正确处理这个问题? ie: It resizes the file somehow while writing data? 即:它在写入数据时以某种方式调整文件大小?

I am assuming you are doing something like, except instead of writing two bytes of data you want to write 4 bytes with some padding. 我假设你正在做类似的事情,除了写入两个字节的数据,你想用一些填充写4个字节。

#include <fstream>

using namespace std;

struct data
{
    char first;
    char second;
};

int _tmain(int argc, _TCHAR* argv[])
{
    ofstream outFile;
    data data1;
    data data2;

    data1.first = 'a';
    data1.second = 'b';
    data2.first = 'c';
    data2.second = 'd';

    outFile.open("somefile.dat");

    outFile.write(reinterpret_cast<char*>(&data1), sizeof(data));
    outFile.write(reinterpret_cast<char*>(&data2), sizeof(data));

    outFile.close();

    return 0;
}

One option is to simply make the struct 4 bytes. 一种选择是简单地使struct 4字节。 This could have a disadvantage as it could increase memory footprint. 这可能有一个缺点,因为它可能会增加内存占用。

Using seekp probably is not a good option, I tried it and it sort of worked but not really. 使用seekp可能不是一个好的选择,我试过它,它有点工作但不是真的。

    outFile.write(reinterpret_cast<char*>(&data1), sizeof(data));
    outFile.seekp(2, ios_base::cur);
    outFile.write(reinterpret_cast<char*>(&data2), sizeof(data));
    outFile.seekp(2, ios_base::cur);

This did succeed in adding padding after data1 but not data2. 这确实成功地在data1之后添加了padding而不是data2。 Moving the pointer past the just isn't a good idea as it doesn't change the file size. 将指针移过just只是不是一个好主意,因为它不会改变文件大小。 I tried writing 0 bytes after seekp but this didn't work either. 我尝试在seekp之后写入0个字节,但这也不起作用。

Honestly I would implement a helper function to provide this functionality. 老实说,我会实现一个辅助函数来提供这个功能。 Seems much cleaner this way. 这种方式似乎更清洁。 Here is a simple example: 这是一个简单的例子:

#include <fstream>

using namespace std;

struct data
{
    char first;
    char second;
};

void WriteWithPadding(ofstream* outFile, data d, int width);

int _tmain(int argc, _TCHAR* argv[])
{
    ofstream* outFile = new ofstream();
    data data1;
    data data2;

    data1.first = 'a';
    data1.second = 'b';
    data2.first = 'c';
    data2.second = 'd';

    outFile->open("somefile.dat");

    WriteWithPadding(outFile, data1, 4);
    WriteWithPadding(outFile, data1, 4);

    outFile->close();
    delete outFile;

    return 0;
}

void WriteWithPadding(ofstream* outFile, data d, int width)
{
    if (sizeof(d) > width)
        throw;

    width = width - sizeof(d); // width is now amount of padding required
    outFile->write(reinterpret_cast<char*>(&d), sizeof(data));

    // Add Padding
    for (int i = 0; i < width; i++)
    {
        outFile->put(0);
    }
}

Just to be pedantic, I assume you have opened your file with ios::binary , because you'll have issues if you haven't. 只是为了迂腐,我假设你已经用ios::binary打开了你的文件,因为如果你没有,你会遇到问题。

When writing a file, the file is only as large as the number of bytes you have written to your file. 编写文件时,该文件只与您写入文件的字节数一样大。 So if you write three bytes to the file, you will have a three-byte file. 因此,如果您向文件写入三个字节,您将拥有一个三字节文件。

To maintain a four-byte resolution, you must make sure to write four bytes at a time -- if you write a three-byte object, write an additional byte (zero?) to bring it up to four bytes. 要保持四字节分辨率,必须确保一次写入四个字节 - 如果编写一个三字节对象,则写入一个额外的字节(零?)以使其最多为四个字节。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM