stream converting

Discussion in 'C++' started by Moritz Tacke, Oct 31, 2003.

  1. Moritz Tacke

    Moritz Tacke Guest

    Hi there!

    I have a problem concerning the file input to a c++ program: the
    expected input is either in binary or in some kind of text format;
    what I finally want is the binary version.
    I know how to translate between these two versions and would like to
    write some kind of wrapper. This wrapper should open the text file and
    provide an istream which behaves exactly like the ifstream of an
    equivalent binary file would. Is there a smart way to do this?
    Greetings,
    Moritz
    Moritz Tacke, Oct 31, 2003
    #1
    1. Advertising

  2. Moritz Tacke

    Mike Wahler Guest

    "Moritz Tacke" <-freiburg.de> wrote in message
    news:...
    > Hi there!
    >
    > I have a problem concerning the file input to a c++ program: the
    > expected input is either in binary or in some kind of text format;
    > what I finally want is the binary version.
    > I know how to translate between these two versions and would like to
    > write some kind of wrapper. This wrapper should open the text file and
    > provide an istream which behaves exactly like the ifstream of an
    > equivalent binary file would. Is there a smart way to do this?


    No 'wrappers' or 'smartness' required:

    std::ifstream input("myfile.txt", std::ios::binary);

    -Mike
    Mike Wahler, Oct 31, 2003
    #2
    1. Advertising

  3. Moritz Tacke

    Moritz Tacke Guest

    >
    > No 'wrappers' or 'smartness' required:
    >
    > std::ifstream input("myfile.txt", std::ios::binary);
    >
    > -Mike


    Well, it looks like I didn't express myself correctly: the binary
    format and the "text"-format are qualitatively different. The text
    format is some kind of encoding of the binary one, where four bytes of
    "text" are used to encode three bytes of the "binary".
    What I am trying to do right now is to write some kind of wrapper
    which reads the four "text"-bytes and converts them to the three
    corresponding binary-bytes; this wrapper should be used by the other
    parts of the program as if it was an ifstream which directly reads the
    binary file.

    Hm. I hope I did succeed in formulating the point this time...
    Greetings!
    Moritz
    Moritz Tacke, Nov 1, 2003
    #3
  4. "Moritz Tacke" <-freiburg.de> wrote in message
    news:...
    ....
    | > std::ifstream input("myfile.txt", std::ios::binary);
    ....
    | Well, it looks like I didn't express myself correctly: the binary
    | format and the "text"-format are qualitatively different. The text
    | format is some kind of encoding of the binary one, where four bytes of
    | "text" are used to encode three bytes of the "binary".
    | What I am trying to do right now is to write some kind of wrapper
    | which reads the four "text"-bytes and converts them to the three
    | corresponding binary-bytes; this wrapper should be used by the other
    | parts of the program as if it was an ifstream which directly reads the
    | binary file.
    Hi Moritz,

    The way to do what you are looking for is to implement a custom
    streambuf class (the polymorphic buffering class which handles
    the i/o behind any standard stream). Your implementation
    of this file would generate the buffer by translating data
    read from another stream.
    For some examples, you may check Dietmar Kühl's website:
    http://www.informatik.uni-konstanz.de/~kuehl/
    Dietmar authored part of Josuttis' (excellent) book
    "The C++ Standard Library - A Tutorial and Reference" (in particular
    sections related to the i/o stream library), a valuable reference.

    But I think that you would be picking the wrong level of
    abstraction by working at the level of the streams.
    It would probably be better to define a higher-level
    interface (abstract base class) to read the contents
    of a file, and implement it for both the ascii and binary
    file formats.
    Then, if needed, you can do the same to write both
    formats, and -- if needed -- perform a translation
    on-the-fly.


    Regards,
    Ivan
    --
    http://ivan.vecerina.com
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Nov 3, 2003
    #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. Rasmusson, Lars
    Replies:
    1
    Views:
    710
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    610
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    440
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    543
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    389
    James Kanze
    May 10, 2007
Loading...

Share This Page