Howdy, Stranger!

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

Categories

C functions & C++ methods

DantyDanty Member Posts: 322
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

  • slicer69slicer69 Member Posts: 272
    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()?
    :

  • DariusDarius Member Posts: 1,666
    : 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

  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    _________ \ http://forcoder.org \ free video tutorials and ebooks about || C#, Delphi, Objective-C, C, Swift, PL/SQL, PHP, MATLAB, Visual Basic, C++, Assembly, JavaScript, Java, Scratch, Go, Python, Visual Basic .NET, Perl, R, Ruby Apex, Scheme, Lua, VBScript, Bash, Clojure, FoxPro, ML, COBOL, Rust, Awk, Logo, Lisp, LabVIEW, F#, Ada, Prolog, SAS, Kotlin, Dart, Fortran, D, Scala, ABAP, Erlang, Transact-SQL, Hack, Crystal, Alice, Julia || ______________

Sign In or Register to comment.