C++ replace data in binary file with fstream












0














I try to replace data in binary file using C++ and fstream library. Is it possible to do that with this library?
I would like to change one byte of file in address: 0xB07 to 1.



I have written the following code snippet:



...

int address = 0xB07;
char * toChange = new char('0');

std::ifstream input(filename, std::ios::binary);
input.seekg(address);
input.read(toChange, 1);
input.close();

*toChange = (char)0x01;

std::ofstream output(filename, std::ios::binary);
output.seekp(address);
output.write(toChange, 1);
output.close();

...


I have tried many versions of this code and I still can't understand why the byte doesn't change.










share|improve this question
























  • As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
    – jakub_d
    Nov 21 '18 at 14:33










  • Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
    – happysadek
    Nov 21 '18 at 14:40










  • Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
    – jakub_d
    Nov 21 '18 at 14:45










  • I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
    – happysadek
    Nov 21 '18 at 15:00










  • Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
    – jakub_d
    Nov 21 '18 at 15:03
















0














I try to replace data in binary file using C++ and fstream library. Is it possible to do that with this library?
I would like to change one byte of file in address: 0xB07 to 1.



I have written the following code snippet:



...

int address = 0xB07;
char * toChange = new char('0');

std::ifstream input(filename, std::ios::binary);
input.seekg(address);
input.read(toChange, 1);
input.close();

*toChange = (char)0x01;

std::ofstream output(filename, std::ios::binary);
output.seekp(address);
output.write(toChange, 1);
output.close();

...


I have tried many versions of this code and I still can't understand why the byte doesn't change.










share|improve this question
























  • As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
    – jakub_d
    Nov 21 '18 at 14:33










  • Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
    – happysadek
    Nov 21 '18 at 14:40










  • Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
    – jakub_d
    Nov 21 '18 at 14:45










  • I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
    – happysadek
    Nov 21 '18 at 15:00










  • Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
    – jakub_d
    Nov 21 '18 at 15:03














0












0








0







I try to replace data in binary file using C++ and fstream library. Is it possible to do that with this library?
I would like to change one byte of file in address: 0xB07 to 1.



I have written the following code snippet:



...

int address = 0xB07;
char * toChange = new char('0');

std::ifstream input(filename, std::ios::binary);
input.seekg(address);
input.read(toChange, 1);
input.close();

*toChange = (char)0x01;

std::ofstream output(filename, std::ios::binary);
output.seekp(address);
output.write(toChange, 1);
output.close();

...


I have tried many versions of this code and I still can't understand why the byte doesn't change.










share|improve this question















I try to replace data in binary file using C++ and fstream library. Is it possible to do that with this library?
I would like to change one byte of file in address: 0xB07 to 1.



I have written the following code snippet:



...

int address = 0xB07;
char * toChange = new char('0');

std::ifstream input(filename, std::ios::binary);
input.seekg(address);
input.read(toChange, 1);
input.close();

*toChange = (char)0x01;

std::ofstream output(filename, std::ios::binary);
output.seekp(address);
output.write(toChange, 1);
output.close();

...


I have tried many versions of this code and I still can't understand why the byte doesn't change.







c++ fstream binaryfiles istream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:51







happysadek

















asked Nov 21 '18 at 14:03









happysadekhappysadek

13




13












  • As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
    – jakub_d
    Nov 21 '18 at 14:33










  • Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
    – happysadek
    Nov 21 '18 at 14:40










  • Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
    – jakub_d
    Nov 21 '18 at 14:45










  • I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
    – happysadek
    Nov 21 '18 at 15:00










  • Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
    – jakub_d
    Nov 21 '18 at 15:03


















  • As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
    – jakub_d
    Nov 21 '18 at 14:33










  • Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
    – happysadek
    Nov 21 '18 at 14:40










  • Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
    – jakub_d
    Nov 21 '18 at 14:45










  • I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
    – happysadek
    Nov 21 '18 at 15:00










  • Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
    – jakub_d
    Nov 21 '18 at 15:03
















As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
– jakub_d
Nov 21 '18 at 14:33




As you have not provided a Minimal, Complete, and Verifiable example that we could try for ourselves, we are limited to your description. So what does "it doesn't work" mean? Do you get some error message? Something unexpected happens? (what is it?)
– jakub_d
Nov 21 '18 at 14:33












Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
– happysadek
Nov 21 '18 at 14:40




Sorry for that. 'It doesn't work' means that the bytes aren't replaced. I can check them for example in Hex Editor.
– happysadek
Nov 21 '18 at 14:40












Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
– jakub_d
Nov 21 '18 at 14:45




Try adding error checking to the write part. Maybe the file is not actually writable? At least it would tell you which part is failing.
– jakub_d
Nov 21 '18 at 14:45












I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
– happysadek
Nov 21 '18 at 15:00




I hope my question is better now. How to add error checking? I have tried to check output.bad() but it returns false.
– happysadek
Nov 21 '18 at 15:00












Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
– jakub_d
Nov 21 '18 at 15:03




Can an ofstream constructor fail? How does it indicate failure? Check for it. Can seekp fail? How does it... (etc.) To answer the Hows, look at the reference cplusplus.com/reference/fstream/ofstream
– jakub_d
Nov 21 '18 at 15:03












1 Answer
1






active

oldest

votes


















0














This code will remove your file and put totally new contents in it.
The problem is in line



std::ofstream output(filename, std::ios::binary);


That's because the default open mode is ios::out | ios::trunc (See for ex.: Input/Output with files)
Which means file is being truncated to zero bytes.



Then your seek() function extends him to the specified size (usually filling with zeroes) and finally output.write() writes the byte at the end.



In order to do what you want I had to specify the following ofstream flags: std::ios::binary|std::ios::out|std::ios::in



I cannot say currently for sure why the std::ios::inis needed, sorry...






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413820%2fc-replace-data-in-binary-file-with-fstream%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This code will remove your file and put totally new contents in it.
    The problem is in line



    std::ofstream output(filename, std::ios::binary);


    That's because the default open mode is ios::out | ios::trunc (See for ex.: Input/Output with files)
    Which means file is being truncated to zero bytes.



    Then your seek() function extends him to the specified size (usually filling with zeroes) and finally output.write() writes the byte at the end.



    In order to do what you want I had to specify the following ofstream flags: std::ios::binary|std::ios::out|std::ios::in



    I cannot say currently for sure why the std::ios::inis needed, sorry...






    share|improve this answer


























      0














      This code will remove your file and put totally new contents in it.
      The problem is in line



      std::ofstream output(filename, std::ios::binary);


      That's because the default open mode is ios::out | ios::trunc (See for ex.: Input/Output with files)
      Which means file is being truncated to zero bytes.



      Then your seek() function extends him to the specified size (usually filling with zeroes) and finally output.write() writes the byte at the end.



      In order to do what you want I had to specify the following ofstream flags: std::ios::binary|std::ios::out|std::ios::in



      I cannot say currently for sure why the std::ios::inis needed, sorry...






      share|improve this answer
























        0












        0








        0






        This code will remove your file and put totally new contents in it.
        The problem is in line



        std::ofstream output(filename, std::ios::binary);


        That's because the default open mode is ios::out | ios::trunc (See for ex.: Input/Output with files)
        Which means file is being truncated to zero bytes.



        Then your seek() function extends him to the specified size (usually filling with zeroes) and finally output.write() writes the byte at the end.



        In order to do what you want I had to specify the following ofstream flags: std::ios::binary|std::ios::out|std::ios::in



        I cannot say currently for sure why the std::ios::inis needed, sorry...






        share|improve this answer












        This code will remove your file and put totally new contents in it.
        The problem is in line



        std::ofstream output(filename, std::ios::binary);


        That's because the default open mode is ios::out | ios::trunc (See for ex.: Input/Output with files)
        Which means file is being truncated to zero bytes.



        Then your seek() function extends him to the specified size (usually filling with zeroes) and finally output.write() writes the byte at the end.



        In order to do what you want I had to specify the following ofstream flags: std::ios::binary|std::ios::out|std::ios::in



        I cannot say currently for sure why the std::ios::inis needed, sorry...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:52









        LiMarLiMar

        1,49621522




        1,49621522






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413820%2fc-replace-data-in-binary-file-with-fstream%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen