Grrr... C++ file I/O

Discussion in 'C++' started by SpreadTooThin, Jun 8, 2007.

  1. Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    I just don't get it.. really!

    filebuf, istream, ostream, ifstream, ofstream, iosteam...
    what the f&(){( do I use to do binary file i/o read and write.
    I need to be able to read and write, seek and tell
    which class do I use?????

    frustrated..
    SpreadTooThin, Jun 8, 2007
    #1
    1. Advertising

  2. SpreadTooThin

    osmium Guest

    "SpreadTooThin" writes:

    > Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    > I just don't get it.. really!
    >
    > filebuf, istream, ostream, ifstream, ofstream, iosteam...
    > what the f&(){( do I use to do binary file i/o read and write.
    > I need to be able to read and write, seek and tell
    > which class do I use?????


    Use ios::binary as a parameter the file open.
    Use ifstream, tellg, seekg and read to read.
    Use ofstream, tellp, seekp, and write to write.

    Salt judiciously with std:: until your head swims.
    osmium, Jun 8, 2007
    #2
    1. Advertising

  3. SpreadTooThin

    Tim Love Guest

    Tim Love, Jun 8, 2007
    #3
  4. SpreadTooThin

    Ron AF Greve Guest

    Hi,

    Simple example

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;

    int main( int ArgC, char *ArgV[] )
    {
    //----------------Input example
    // bin just to make sure it works the same on ms-windows and unix flavors
    (unix'es are always bin)
    ifstream Input( "Filename.dat", ios_base::binary );
    if( !Input.is_open() )
    {
    cerr << __LINE__ << "see an error" << endl;
    throw "Error";
    }
    string Line;
    while( getline( Input, Line ) )
    {
    cout << "Read <" << Line << ">" << endl;
    }

    //-------------------Output example
    ofstream Output( "Filenameout.dat", ios_base::binary );
    if( !Output.is_open() )
    {
    cerr << __LINE__ << "see an error" << endl;
    throw "Error";
    }
    // Right left alligned space right padded long as string
    long Number = 10;
    // The following writes text
    Output << setiosflags( ios_base::left ) << resetiosflags(
    ios_base::right ) << setw( 20 ) << setfill( ' ' ) << Number << endl;

    // This wil write 4 bytes on most 32 bit machines
    // Write arbitrary data for instance long
    Output.write( reinterpret_cast<char*>( &Number ), sizeof( Number ) ); //
    Opposite is Input.read


    }

    Regards, Ron AF Greve

    http://www.InformationSuperHighway.eu

    "SpreadTooThin" <> wrote in message
    news:...
    > Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    > I just don't get it.. really!
    >
    > filebuf, istream, ostream, ifstream, ofstream, iosteam...
    > what the f&(){( do I use to do binary file i/o read and write.
    > I need to be able to read and write, seek and tell
    > which class do I use?????
    >
    > frustrated..
    >
    Ron AF Greve, Jun 8, 2007
    #4
  5. On Jun 8, 3:00 pm, "osmium" <> wrote:
    > "SpreadTooThin" writes:
    > > Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    > > I just don't get it.. really!

    >
    > > filebuf, istream, ostream, ifstream, ofstream, iosteam...
    > > what the f&(){( do I use to do binary file i/o read and write.
    > > I need to be able to read and write, seek and tell
    > > which class do I use?????

    >
    > Use ios::binary as a parameter the file open.
    > Use ifstream, tellg, seekg and read to read.
    > Use ofstream, tellp, seekp, and write to write.
    >
    > Salt judiciously with std:: until your head swims.


    Am I correct the tellg resets the stream to the begining of the file?
    SpreadTooThin, Jun 8, 2007
    #5
  6. On Jun 8, 3:00 pm, "osmium" <> wrote:
    > "SpreadTooThin" writes:
    > > Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    > > I just don't get it.. really!

    >
    > > filebuf, istream, ostream, ifstream, ofstream, iosteam...
    > > what the f&(){( do I use to do binary file i/o read and write.
    > > I need to be able to read and write, seek and tell
    > > which class do I use?????

    >
    > Use ios::binary as a parameter the file open.
    > Use ifstream, tellg, seekg and read to read.
    > Use ofstream, tellp, seekp, and write to write.
    >
    > Salt judiciously with std:: until your head swims.


    The header files <iostream>, <fstream> etc..
    I mean what is iostream for?
    why not just fstream or ifstream for ifstream and ofstream for
    ofstream?
    SpreadTooThin, Jun 8, 2007
    #6
  7. SpreadTooThin

    osmium Guest

    "SpreadTooThin" writes:

    > Am I correct the tellg resets the stream to the begining of the file?


    No. I forgot to mention, g stands for get and p stand for put; there are
    two pointers into the file. seekg(0) and seekp(0) will get you to the
    beginning of the file, may be syntax errors, I am writing from memory.
    osmium, Jun 8, 2007
    #7
  8. On 2007-06-08 23:55, SpreadTooThin wrote:
    > On Jun 8, 3:00 pm, "osmium" <> wrote:
    >> "SpreadTooThin" writes:
    >> > Damn.. FILE *fp was just sooo much easier than C++ file i/o...
    >> > I just don't get it.. really!

    >>
    >> > filebuf, istream, ostream, ifstream, ofstream, iosteam...
    >> > what the f&(){( do I use to do binary file i/o read and write.
    >> > I need to be able to read and write, seek and tell
    >> > which class do I use?????

    >>
    >> Use ios::binary as a parameter the file open.
    >> Use ifstream, tellg, seekg and read to read.
    >> Use ofstream, tellp, seekp, and write to write.
    >>
    >> Salt judiciously with std:: until your head swims.

    >
    > Am I correct the tellg resets the stream to the begining of the file?


    No, tellg() tells you where you are, if you want to go somewhere use
    seekg(). For more documentation about streams have a look at
    www.cplusplus.com

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 8, 2007
    #8
  9. On Jun 8, 4:48 pm, "osmium" <> wrote:
    > "SpreadTooThin" writes:
    > > Am I correct the tellg resets the stream to the begining of the file?

    >
    > No. I forgot to mention, g stands for get and p stand for put; there are
    > two pointers into the file. seekg(0) and seekp(0) will get you to the
    > beginning of the file, may be syntax errors, I am writing from memory.


    But in no circumstance will tell reposition the file pointer?
    SpreadTooThin, Jun 8, 2007
    #9
  10. SpreadTooThin

    James Kanze Guest

    On Jun 9, 12:48 am, "osmium" <> wrote:
    > "SpreadTooThin" writes:
    > > Am I correct the tellg resets the stream to the begining of the file?


    > No. I forgot to mention, g stands for get and p stand for put; there are
    > two pointers into the file. seekg(0) and seekp(0) will get you to the
    > beginning of the file, may be syntax errors, I am writing from memory.


    No there aren't. In general, at the [io]stream level, it is
    unspecified whether a bi-directional stream maintains two
    pointers, or one. The interface is designed to support two,
    since some streams (e.g. stringstream) do support two, but
    changing the read position (seekg()) on a fstream will also
    change the write position.

    Be aware, too, that all of the restrictions as to what
    positionning is legal on a FILE* also apply to [io]stream. In
    practice, on a text stream, you can only seek to the beginning,
    or to a place where you've been before, and whose position you
    obtained using tell[gp]. (And of course tell[gp] never changes
    the position in the file.)

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 9, 2007
    #10
  11. SpreadTooThin wrote:
    > Damn.. FILE *fp was just sooo much easier than C++ file i/o...


    Then why don't you use it? It's not like it doesn't work in C++.
    Juha Nieminen, Jun 9, 2007
    #11
  12. On Sat, 09 Jun 2007 11:17:30 +0300, Juha Nieminen wrote:
    >SpreadTooThin wrote:
    >> Damn.. FILE *fp was just sooo much easier than C++ file i/o...

    >
    > Then why don't you use it? It's not like it doesn't work in C++.


    I always prefer stdio to iostream if I have the choice. It's also not
    difficult to wrap FILE* and the functions into a safer class.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Jun 9, 2007
    #12
  13. On Jun 9, 2:17 am, Juha Nieminen <> wrote:
    > SpreadTooThin wrote:
    > > Damn.. FILE *fp was just sooo much easier than C++ file i/o...

    >
    > Then why don't you use it? It's not like it doesn't work in C++.


    I guess part of it is keeping up with the times (even if C++ is not
    a spring chicken in terms of technological age.) and being able to
    look at other peoples code and understand it. The same question may
    be why C++ if you liked C. But yes I understand where you are comming
    from.

    B.
    SpreadTooThin, Jun 9, 2007
    #13
  14. Roland Pibinger wrote:
    > I always prefer stdio to iostream if I have the choice.


    The problem with stdio is that it's not abstract enough.
    Example:

    typedef int ValueType;
    ....
    ValueType i = 4;
    printf("%i\n", i); // this times 100 in a large program.

    Later you notice that int is actually not enough, and you are
    compiling for a 64-bit system, so you change:

    typedef long ValueType;

    and all your printfs break.

    Ok, you could use a kludge and say:

    typedef int ValueType;
    #define VALUE_TYPE_FORMAT_STR "%i"
    ....
    ValueType i = 4;
    printf(VALUE_TYPE_FORMAT_STR"\n", i);

    But then in the future you notice that actually int nor even long
    is enough, so you replace the typedef with

    class ValueType { ... }; // eg. a 256-bit integer

    and all your printfs break.
    Juha Nieminen, Jun 9, 2007
    #14
  15. SpreadTooThin

    James Kanze Guest

    On Jun 9, 10:17 am, Juha Nieminen <> wrote:
    > SpreadTooThin wrote:
    > > Damn.. FILE *fp was just sooo much easier than C++ file i/o...


    > Then why don't you use it? It's not like it doesn't work in
    > C++.


    Why would it work in C++? It didn't work in C, at least for
    formatted IO.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 9, 2007
    #15
  16. SpreadTooThin

    Alan Johnson Guest

    James Kanze wrote:
    > On Jun 9, 10:17 am, Juha Nieminen <> wrote:
    >> SpreadTooThin wrote:
    >>> Damn.. FILE *fp was just sooo much easier than C++ file i/o...

    >
    >> Then why don't you use it? It's not like it doesn't work in
    >> C++.

    >
    > Why would it work in C++? It didn't work in C, at least for
    > formatted IO.


    Because in C++ you can wrap the FILE* in a class. Maybe even overload
    an operator to automatically format the primitive types for you before
    writing. And then clients could also overload that operator to format
    their own types. Maybe operator<< would be a good choice.

    --
    Alan Johnson
    Alan Johnson, Jun 10, 2007
    #16
  17. On Sat, 09 Jun 2007 20:37:41 +0300, Juha Nieminen wrote:
    > The problem with stdio is that it's not abstract enough.
    >Example:


    You example indicates that stdio may not be type-safe enough (not that
    it's not abstract enough).

    >typedef int ValueType;
    >...
    >ValueType i = 4;
    >printf("%i\n", i); // this times 100 in a large program.
    >
    > Later you notice that int is actually not enough, and you are
    >compiling for a 64-bit system, so you change:
    >
    >typedef long ValueType;
    >
    >and all your printfs break.
    >
    > Ok, you could use a kludge and say:
    >
    >typedef int ValueType;
    >#define VALUE_TYPE_FORMAT_STR "%i"
    >...
    >ValueType i = 4;
    >printf(VALUE_TYPE_FORMAT_STR"\n", i);


    Your integer value has a specific meaning in the current context which
    could be expressed by a type-safe wrapper function, e.g.

    inline
    int printAmount (int i) {
    return printf("%i\n", i);
    }

    > But then in the future you notice that actually int nor even long
    >is enough, so you replace the typedef with
    >
    >class ValueType { ... }; // eg. a 256-bit integer
    >
    >and all your printfs break.


    In theory you can write an operator<< for ValueType, in practice you
    don't know a general purpose format to write one and need funtions for
    different formatted outputs anyway.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Jun 10, 2007
    #17
  18. Roland Pibinger wrote:
    > Your integer value has a specific meaning in the current context which
    > could be expressed by a type-safe wrapper function, e.g.
    >
    > inline
    > int printAmount (int i) {
    > return printf("%i\n", i);
    > }


    That only works if you want to print the value to stdout with that
    exact format. What if you want to print it with another format (and
    possibly alongside other values and text) and for example to a
    char array instead of stdout?
    Juha Nieminen, Jun 10, 2007
    #18
  19. On Sun, 10 Jun 2007 14:01:46 +0300, Juha Nieminen wrote:
    >Roland Pibinger wrote:
    >> Your integer value has a specific meaning in the current context which
    >> could be expressed by a type-safe wrapper function, e.g.
    >>
    >> inline
    >> int printAmount (int i) {
    >> return printf("%i\n", i);
    >> }

    >
    > That only works if you want to print the value to stdout with that
    >exact format. What if you want to print it with another format (and
    >possibly alongside other values and text) and for example to a
    >char array instead of stdout?


    It depends on the number of print requirements in your application.
    Unless you need hundreds of different formats small wrapper functions
    are appropriate. The 'backend' functions (printf, fprintf, snprintf)
    are ready for use and wrappers are implemented quickly. At least I
    prefer functions like printDateISO() etc.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
    Roland Pibinger, Jun 10, 2007
    #19
  20. SpreadTooThin

    Lionel B Guest

    On Sat, 09 Jun 2007 08:36:22 +0000, Roland Pibinger wrote:

    > On Sat, 09 Jun 2007 11:17:30 +0300, Juha Nieminen wrote:
    >>SpreadTooThin wrote:
    >>> Damn.. FILE *fp was just sooo much easier than C++ file i/o...

    >>
    >> Then why don't you use it? It's not like it doesn't work in C++.

    >
    > I always prefer stdio to iostream if I have the choice. It's also not
    > difficult to wrap FILE* and the functions into a safer class.


    I believe that has been done before... for one such implementation see
    the standard fstream classes ;-)

    --
    Lionel B
    Lionel B, Jun 11, 2007
    #20
    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. Mike Kingscott
    Replies:
    0
    Views:
    533
    Mike Kingscott
    Dec 5, 2003
  2. Elsa Luiz

    Parent window keeps the focus! grrr

    Elsa Luiz, Apr 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    682
    Elsa Luiz
    Apr 22, 2004
  3. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,393
    Scott Allen
    Jun 4, 2005
  4. Tim Streater
    Replies:
    22
    Views:
    1,050
    dorayme
    Jul 8, 2007
  5. TonyV
    Replies:
    19
    Views:
    744
    Mark Clements
    Mar 19, 2007
Loading...

Share This Page