Re: Saving a binary file into a string

Discussion in 'C++' started by Rune Allnor, Dec 27, 2009.

  1. Rune Allnor

    Rune Allnor Guest

    On 27 Des, 16:39, Dominik Schmidt <> wrote:
    > Hi,
    >
    > I'm new to C++, so I have a very basic question.
    > I wrote a function which opens a file and saves it into a string variable.
    > Another function can save a string variable into a file.
    > I tried to combine those two functions, because in combination they should
    > make an exact copy of a file. And it seems to work, in every case the copy
    > had the exact same hash value (MD5) as the original file.
    >
    > I'd like to know if I made an error, so if there's any possibility that my
    > functions won't work in some case?


    There is the possibility, yes.

    Some byte values or sequences of byte values take on special
    meanings like 'end of line' or 'end of string' in the context
    of text files and strings.

    If you want to work with binary files, use std::vector<char>
    or something like that, instead of std::string. That way all
    characters are treated as arbitrary numbers, with no special
    significance attached to any of them.

    Rune
     
    Rune Allnor, Dec 27, 2009
    #1
    1. Advertising

  2. Rune Allnor

    Rune Allnor Guest

    On 27 Des, 19:11, Dominik Schmidt <> wrote:

    > > If you want to work with binary files, use std::vector<char>
    > > or something like that, instead of std::string. That way all
    > > characters are treated as arbitrary numbers, with no special
    > > significance attached to any of them.

    >
    > Actually I thought my code would already do this by using "ios::binary" in
    > both functions?


    It might have done, but by using std::string you confuse readers
    of your code about what you are doing. std::string is intended
    for human-readable text, and anywhere one sees it, one expects
    to handle human-readable text. You break with that expectation,
    which will only cause confusion later on. You also expose your
    data to functions and manipulations intended for strings.

    If you use std::vector<char> you first of all make it clear to
    the reader that these are binary data, and the reader of your
    code will relate to your code accordingly. Secondly you prevent
    your data from being accessed through string-manipulating
    functions. Which would only have caused mayhem and misery,
    if it occured.

    The key to C++ is to express the data in terms of the type
    or class that best represents the properties of the data.

    Since you do *not* have text data, std::string, which is
    intended for precisely text data, is the *wrong* container
    to use.

    Rune
     
    Rune Allnor, Dec 27, 2009
    #2
    1. Advertising

  3. Rune Allnor

    James Kanze Guest

    On Dec 27, 6:11 pm, Dominik Schmidt <> wrote:
    > On Sun, 27 Dec 2009 09:39:12 -0800 (PST), Rune Allnor wrote:


    [...]
    > > If you want to work with binary files, use std::vector<char>
    > > or something like that, instead of std::string. That way all
    > > characters are treated as arbitrary numbers, with no special
    > > significance attached to any of them.


    > Actually I thought my code would already do this by using
    > "ios::binary" in both functions?


    It should. There could still formally be a problem, since
    formally, implementations aren't required to recognize the end
    of file correctly for binary files; e.g. you might read 128
    bytes, when the input really only contained 10. But in
    practice, systems where this would occur are almost non-existent
    today.

    --
    James Kanze
     
    James Kanze, Dec 28, 2009
    #3
  4. In message
    <>,
    Rune Allnor <> writes
    >On 27 Des, 16:39, Dominik Schmidt <> wrote:
    >> Hi,
    >>
    >> I'm new to C++, so I have a very basic question.
    >> I wrote a function which opens a file and saves it into a string variable.
    >> Another function can save a string variable into a file.
    >> I tried to combine those two functions, because in combination they should
    >> make an exact copy of a file. And it seems to work, in every case the copy
    >> had the exact same hash value (MD5) as the original file.
    >>
    >> I'd like to know if I made an error, so if there's any possibility that my
    >> functions won't work in some case?

    >
    >There is the possibility, yes.
    >
    >Some byte values or sequences of byte values take on special
    >meanings like 'end of line' or 'end of string' in the context
    >of text files


    Yes. But the OP is using binary files, so that's irrelevant.

    >and strings.


    No. std::string can contain arbitrary sequences of characters without
    any constraints on their values, and doesn't impose any semantics on
    them.

    (The only potential problem is that '\0' is treated as a terminator when
    assigning or constructing std::strings from C-style null-terminated
    strings, i.e. when calling member functions or operators which take a
    single const char * argument. The corresponding functions which take two
    iterators (or pointers) don't share this problem.)

    >
    >If you want to work with binary files, use std::vector<char>
    >or something like that, instead of std::string.


    Probably good advice, since it makes the intent clearer, but not for the
    stated reason:

    > That way all
    >characters are treated as arbitrary numbers, with no special
    >significance attached to any of them.
    >


    --
    Richard Herring
     
    Richard Herring, Jan 15, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,536
    Luis Esteban Valencia
    Jan 6, 2005
  2. G-Factor

    Saving a binary file help.

    G-Factor, Jul 7, 2003, in forum: C++
    Replies:
    7
    Views:
    446
    Thomas Matthews
    Jul 7, 2003
  3. Jonathan Lee
    Replies:
    1
    Views:
    372
    Jonathan Lee
    Dec 27, 2009
  4. Jorgen Grahn
    Replies:
    4
    Views:
    444
    Kaz Kylheku
    Dec 29, 2009
  5. Kaz Kylheku
    Replies:
    1
    Views:
    391
    Fred Zwarts
    Dec 28, 2009
Loading...

Share This Page