0

I'm reading a file in binary mode with std::ifstream. Here is the way I'm reading from the file:

enum class Resources : uint64_t
{
    Resource_1 = 0xA97082C73B2BC4BB,
    Resource_2 = 0xB89A596B420BB2E2,
};

struct ResourceHeader
{
    Resources hash;
    uint32_t size;
};

int main()
{
    std::ifstream input(path, std::ios::binary);
    while (true)
    {
        if (input.eof()) break;
        ResourceHeader RCHeader{};
        input.read((char*)&RCHeader, sizeof(ResourceHeader));

        uint16_t length = 0;
        input.read((char*)&length, sizeof(uint16_t));
        std::string str;
        str.resize(length);
        input.read(&str[0], length); /* here I get a string from binary file */

        /* and some other reading */
    }
}

After reading, I want to make some changes in some data that I read from the file, and after all changes then write the changed data in a new file.

So I want to know, how can I store the edited char into a buffer (BTW, I don't know the exact size of the buffer with edits)? Also, I need to be able to go back and forth in new created buffer and edit some data again.

So, how can I achieve this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
file-tracer
  • 329
  • 1
  • 7
  • 4
    [Why is `if (input.eof()) break;` wrong?](https://stackoverflow.com/a/5605159/4641116) – Eljay Aug 08 '21 at 19:07
  • 2
    Store the data however you want. For instance, in a `std::vector`. Then you can iterate through it when needed, reversing the shown code to write the data to a new file. – Remy Lebeau Aug 08 '21 at 19:10
  • 1
    @Eljay Because I followed this https://stackoverflow.com/a/21656/13105822 maybe its wrong? – file-tracer Aug 08 '21 at 19:11
  • 1
    @RemyLebeau so I should use vector and save the data as char everytime (with push_back) and for go back and forth go to the index number I want? also how can I store a `struct` in a vector? – file-tracer Aug 08 '21 at 19:15
  • 5
    No, it's not wrong. It is correct. But that's not how your code checks `eof`. If you pay very, very careful attention, that code checks `eof()` ***after it tries to read input***, and not ***before***, like your code does. Details matter, in C++. – Sam Varshavchik Aug 08 '21 at 19:17
  • 2
    the important detail is whether you use the read input before or after the check, because you only know that you are at `eof` *after* the last attempt to read failed – 463035818_is_not_an_ai Aug 08 '21 at 19:18
  • 2
    what does the data represent? what modifications do you want to apply? Maybe there is some de-/serialization involved? – 463035818_is_not_an_ai Aug 08 '21 at 19:21
  • 1
    @463035818_is_not_a_number data is chunked, each chunk have its own magic that represent what kind of data stored in it (most of chunks are just strings) the structure of chunk itself is. `uint64_t magic; uint32_t size;` I just want to change the strings inside of the chunks (structure of string table is `uint16_t length; char[length]` until the end of chunk. so for editing the file and create a new file based of that I need to first update both string and size of string and at the end change the chunk size – file-tracer Aug 08 '21 at 19:39
  • 1
    @SamVarshavchik oh thank you, I get it, so ether I need to check the `eof` after reading it, or use the method that you mention in first comment – file-tracer Aug 08 '21 at 19:43
  • 2
    @file-tracer don't use `char[]`, use `std::string`. And you store a `struct` in a `vector` the same way you store any other kind of data. – Remy Lebeau Aug 08 '21 at 20:38
  • 1
    @RemyLebeau I used the `char[]` to only show the structure, as you can see in question I already using the `std::string` – file-tracer Aug 08 '21 at 21:04
  • 1
    @RemyLebeau sorry for asking again, but can You please add a example of using `std::vector` for this job? I mean how can I determine the size of it? (or maybe I dont need to?) after all I will be grateful if you can add an example. – file-tracer Aug 08 '21 at 22:37
  • 1
    I mean how can I write directly to `vector` without using `push_back` (by directly i mean write from ifstream)? https://stackoverflow.com/a/36649444/13105822 - I can first read it and then push it back to vector (+ cast to `char*`) but I think its not a good Idea – file-tracer Aug 08 '21 at 23:21

0 Answers0