Re: Saving a binary file into a string

Discussion in 'C++' started by Jonathan Lee, Dec 27, 2009.

  1. Jonathan Lee

    Jonathan Lee Guest

    On Dec 27, 10:39 am, Dominik Schmidt <> wrote:
    > Just to be sure: 1 Byte can have 2^8 = 256 different states, so each
    > "character" of a string variable can have those 256 states, right?


    Strictly speaking, char may be larger than an octet in C++. The
    value of CHAR_BIT in <climits> will tell you exactly how big. I
    think both POSIX and Windows require it to be 8, though. And even
    if it weren't, I don't think your code would be affected as is.

    > string OpenFile(string FilePath)


    Nothing major.
    - Str1 seems completely unnecessary. You could write the attach
    function to work with chars.
    - long long isn't an official type in C++ yet. C++0x will add
    it as a type.
    - I don't see why you don't just read the whole file instead of
    a char at a time. That's gotta be slow. If you aren't
    expecting the files to be large, do whatever processing you
    have to in memory.

    > void SaveFile(string FilePath, string FileContent)


    Again, you could write all of FileContent.data() at once.

    > long long GetFileSize(string FilePath)


    I would probably use the return type of tellg() (i.e.,
    std::streampos). No biggy.

    --Jonathan
    Jonathan Lee, Dec 27, 2009
    #1
    1. Advertising

  2. Jonathan Lee

    Jonathan Lee Guest

    On Dec 27, 3:40 pm, Dominik Schmidt <> wrote:
    > But could this cause trouble if I'm writing an application for UN*X (or
    > another common OS)? I guess no?


    Linux and BSDs follow POSIX which requires CHAR_BIT == 8. I don't
    really
    know about other OS's.

    > > - long long isn't an official type in C++ yet. C++0x will add
    > >   it as a type.

    > long long isn't an official type?


    Not in the current version of C++. Though most compilers support it
    as an extension since it's in C99. See here:

    http://en.wikipedia.org/wiki/C++0x#Type_long_long_int
    Or http://www2.research.att.com/~bs/C 0xFAQ.html#long-long

    > What alternative do I have to save a large integer number?


    long, or unsigned long. If you're on a 64-bit OS these will
    be 64-bits anyway. But see below about streampos/tellg()

    > so which official data type could save this number?


    None. Though long *could* be large enough.

    > Can I get into trouble if I continue using long long?


    Not really. It'll be in C++0x which is on the horizon, and
    I think most/all compilers support it as an extension. I
    don't think it's a problem most people get upset about. I
    was just giving you a heads up, really.

    > Is there a significant time difference when using large
    > (maybe > 500 MB) files?


    Probably not significant. Your OS should cache the reads
    from disk so that you aren't going to disk for each char.
    That would be the big performance penalty. You'll just
    incur the call overhead. This is all platform dependent.
    You could test to find out the difference on your
    computer.

    But still, it just seems odd. If you follow Rune's advice
    and use a std::vector, just resize it to the file size
    and read directly into the vector:

    yourvector.resize(filesize) // make room
    yourfile.read(&yourvector[0], filesize); // done.

    > OK, I assume this would be a better way of coding?
    > Can I do anything wrong by using long long here?


    I'm just anal about types. tellg() returns a streampos,
    which is defined by the standard to be a signed integral
    type. So it will fit into long long; you won't have a
    problem with that.

    But also keep in mind that you don't really need
    long long, then. The value in your long long vars will
    never be larger than what can fit in a streampos (which
    in turn will be no larger than a long). So if it were
    me, I'd just get rid of long long and use streampos.
    I'd kill two "problems" with one stone.

    --Jonathan
    Jonathan Lee, Dec 27, 2009
    #2
    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,491
    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:
    432
    Thomas Matthews
    Jul 7, 2003
  3. Rune Allnor
    Replies:
    3
    Views:
    361
    Richard Herring
    Jan 15, 2010
  4. Jorgen Grahn
    Replies:
    4
    Views:
    427
    Kaz Kylheku
    Dec 29, 2009
  5. Kaz Kylheku
    Replies:
    1
    Views:
    371
    Fred Zwarts
    Dec 28, 2009
Loading...

Share This Page