CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 19th, 2009, 10:34 AM
    forumuser11@gmail.com forumuser11@gmail.com is offline
    Member
     
    Join Date: Nov 2008
    Posts: 37
    forumuser11@gmail.com is an unknown quantity at this point (<10)
    C++ File handling VS C file Handling query

    If the fclose is not used in case of C, file remains open in the memory. In case of C++, fclose is called automatically and there is no memory leak.

    Are there any plus/minus points of C++ file handling library (ifstream,ofstream) over C file handling library? Pl explain with example if possible.
    Reply With Quote
      #2    
    Old November 19th, 2009, 10:42 AM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,959
    Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Re: C++ File handling VS C file Handling query

    Quote:
    Originally Posted by forumuser11@gmail.com View Post
    If the fclose is not used in case of C, file remains open in the memory. In case of C++, fclose is called automatically
    The fclose() function works the same way in C as it does C++. If you use fopen() in C++, you have to call fclose() yourself -- it isn't automatic.
    Quote:
    Are there any plus/minus points of C++ file handling library (ifstream,ofstream) over C file handling library?
    Streams can be overloaded, while fopen() and fclose() cannot be overloaded.
    Quote:
    Pl explain with example if possible.
    What's wrong with googling all of this info?

    Regards,

    Paul McKenzie
    Reply With Quote
      #3    
    Old November 19th, 2009, 02:02 PM
    ninja9578 ninja9578 is offline
    Member +
     
    Join Date: Jan 2009
    Posts: 512
    ninja9578 will become famous soon enough (65+)
    Re: C++ File handling VS C file Handling query

    Try this:

    Code:
    struct FileOpen{
       FileOpen(FILE &* f, const char * name, const char * mode){
          f = fopen(name, mode);
          fp = f;
       }
       ~FileOpen(void){
          if (fp) fclose(fp);
       }
       FILE * fp;
    };
    
    FILE * myfile;
    FileOpen fo(myfile, "c:\\test.txt", "w");
    if (!myfile) return;
    That code will automatically close your file for you.
    Reply With Quote
      #4    
    Old November 19th, 2009, 04:15 PM
    Lindley Lindley is online now
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,903
    Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+) Lindley is a name known to all (1000+)
    Re: C++ File handling VS C file Handling query

    There are two primary advantages to streams over the printf family of functions:

    1) Type safety
    2) Polymorphism

    By the first, I mean that it's impossible to have the types of errors you could get with printf, where you try to output a double using %d, and get garbage.

    By the second, I mean that a FILE* is a FILE*, and you can't use it for much else. With streams, an ofstream is also an ostream, and you can define your own objects which are also ostreams, and all of your IO code can be written to ostreams and istreams with no regard to where the data is actually *going*.

    Now, there are some advantages to the printf family over streams, as well:

    1) Marginally faster in some cases (usually not a concern; IO is notoriously slow anyway)
    2) More compact syntax can make the intended operation clearer
    3) scanf() is capable of trying to match specific characters, which is a capability that istreams don't have (you'd need a regex package).
    Reply With Quote
      #5    
    Old November 20th, 2009, 12:05 AM
    laserlight laserlight is online now
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: C++ File handling VS C file Handling query

    Quote:
    Originally Posted by Lindley
    2) More compact syntax can make the intended operation clearer
    However, this can be mitigated by the use of the Boost.Format library.
    __________________
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:40 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.