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 at once.

    > long long GetFileSize(string FilePath)

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

    Jonathan Lee, Dec 27, 2009
    1. Advertisements

  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
    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:

    > 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

    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[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 Lee, Dec 27, 2009
    1. Advertisements

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
    Luis Esteban Valencia
    Jan 6, 2005
  2. G-Factor

    Saving a binary file help.

    G-Factor, Jul 7, 2003, in forum: C++
    Thomas Matthews
    Jul 7, 2003
  3. Replies:
  4. Replies:
    Juha Nieminen
    Jun 17, 2007
  5. Rune Allnor
    Richard Herring
    Jan 15, 2010
  6. Jorgen Grahn
    Kaz Kylheku
    Dec 29, 2009
  7. Kaz Kylheku
    Fred Zwarts
    Dec 28, 2009
  8. pablitoman
    Mar 26, 2010