C functions & C++ methods

hi,
when programming in C++, should one use what (s)he learned in C, i.e., FILE * is used in C along with fwrite(), fread(). Or just use what comes with C++: ofstream() and ifstream()?

Comments

  • Your code will be more portable, run faster and
    probably be easier to read if you use the
    C FILE* functions. However, some (silly) people
    and intructors rather see the new C++ methods.

    : hi,
    : when programming in C++, should one use what (s)he learned in C, i.e., FILE * is used in C along with fwrite(), fread(). Or just use what comes with C++: ofstream() and ifstream()?
    :

  • : Your code will be more portable, run faster and
    : probably be easier to read if you use the
    : C FILE* functions. However, some (silly) people
    : and intructors rather see the new C++ methods.
    :
    : : hi,
    : : when programming in C++, should one use what (s)he learned in C, i.e., FILE * is used in C along with fwrite(), fread(). Or just use what comes with C++: ofstream() and ifstream()?
    : :
    :
    :

    Both "come with" C++, the C Standard Library is part of the C++ standard. However, if you have some background in programming, use whichever you'll feel more comfortable with, otherwise use iostreams.

    Contrary to what slicer69 thinks, about the ONLY benefit of C-style IO for formatted output is conciseness (which isn't much of a benefit because it comes at the cost of readability in most cases). It is also somewhat more portable, but only if you want EXTREME portability. The current GNU C++ compiler handles iostreams and can target just every major OS/architecture out there (and then some) and can always be extended simply by implementing a backend for any that it doesn't support.

    The benefits of iostreams are:

    readability: for most things, definitely for non-C programmers

    ease of learning: no format specifiers to memorize

    speed: static type-checking, no parsing of format specifier, potential inlining

    extensibility: ability to add user defined types and manipulators

    internationalization: it handles internationalization/localization in a cleaner way than C-style IO

    framework extensibility: make new streams, slightly extend old, make new buffers

    generic: they are templated on character type allowing transparent handling of complex character types (e.g. MBCS)

    support exceptions: typically you wont really use it much, but it is there

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories